Comparison of Java and SCL

Language constructions and patterns

0.5 + x * (2e-3 + x * 2)
0.5 + x * (2e-3 + x * 2)
x >= 0 && x <= 1
x >= 0 && x <= 1
Math.sin(a1) - Math.cos(a2)
sin a1 - cos a2
cond ? value : alternative
if cond then value else alternative
"I am " + age + " years old."
"I am " + show age + " years old."
In future:
"I am ${age} years old."
final int sum = a + b;
sum = a + b
public static String decorate(String x) {
    return "(" + x + ")"; 
}
decorate :: String -> String
decorate x = "(" + x + ")"
(the first line is optional)
{
    statement1;
    statement2;
    statement3;
}
do
    statement1
    statement2
    statement3
if(cond1)
    statement1;
else if(cond2)
    statement2;
else 
    statement3;
if cond1
then statement1
else if cond2
then statement2
else statement3
for(T x : l)
    statement;
for l (\x -> 
    statement
)
while(cond) statement;
while (\() -> cond) (\() -> statement)
In future with improvements to macros:
while cond statement
int var;
switch(value) {
case Label1: 
    var = case1; 
    break;
case Label2: 
    var = case2; 
    break;
default:
    var = case3;        
}
var = match value with
    Label1 -> case1
    Label2 -> case2
    _      -> case3
break;
continue;
return x; // In the middle of the code
No exact correspondence. However, every control sequence can be written in the following form:
label1 initialState
  where
    label1 state = 
        ...
        then label2 modifiedState1
        else label3 modifiedState2
    label2 state = 
        ...
    label3 state = 
        ...
        label1 modifiedState
int a, b;
if(cond) {
    a = aExp1;
    b = bExp1;
}
else {
    a = aExp2;
    b = bExp2;
}
(a, b) = if cond
          then (aExp1, bExp1)
          else (aExp2, bExp2)         
int a = 1;
a = 2;
a = 3;
return a;
a = ref 1
a := 2
a := 3
getRef a
The name of the function getRef will change in the future and is probably replaced by some special syntax. For example in ML getRef a would be written as !a.
public static String f(int x) {
    if(x >= 0)
        return Integer.toString(x);
    else
        return null;
}
f :: Integer -> Maybe String
f x | x >= 0    = Just (show x)
     | otherwise = Nothing
public static int invF(String s) {
    if(s == null)
        return -1;
    else
        return Integer.parseInt(s);
}
invF :: Maybe String -> Integer
invF Nothing  = -1
invF (Just s) = read s
Arrays.asList(new String[] {"x", "y", "z"})
["x", "y", "z"]
l.get(3)
l!3
List<String> decorated = new ArrayList<String>();
for(String s : undecorated)
    decorated.add("(" + s + ")");
decorated = ["(" + s + ")" | s <- undecorated]
or
decorated = map (\s -> "(" + s + ")") undecorated
List<String> nonempty = new ArrayList<String>();
for(String s : l)
    if(!s.isEmpty())
        nonempty.add(l);
decorated = [s | s <- l, s != ""]
or
decorated = filter (\s -> s != "") l
List<List<String>> listOfLists;
List<String> flattened 
    = new ArrayList<String>();
for(List<String> l : listOfLists)
    for(String s : l)
        flattened.add(s);
flattened = [s | l <- listOfLists, s <- l]
or
flattened = sum listOfLists
or
flattened = join listOfLists
throw new RuntimeException("Unhandled case.");
fail "Unhandled case."
try {
    ...
    throw new ExceptionX(...);
    ...
} catch(ExceptionX e) {
    ...
} finally {
    ...
}
Currently no correspondence. Exceptions need to be thrown and catched in Java code.
synchronized(obj) {
    ...
}
Currently no correspondence. Synchronization must be done in Java code.
final static class Vec3 {
    public final double x;
    public final double y;
    public final double z;
    
    public Vec3(double x, 
                double y, 
                double x) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}
data Vec3 = Vec3 Double Double Double
In future also:
data Vec3 = Vec3 {
  x :: Double,
  y :: Double,
  z :: Double
}
public static interface Num {}
public static class Int implements Num {
    public static final int value;
    public Int(int value) {
        this.value = value;
    }  
}
public static class Real implements Num {
    public static final double value;
    public Real(double value) {
        this.value = value;
    } 
}
data Num = Int Integer
          | Real Double
public static Num add(Num a_, Num b_) {
    if(a_ instanceof Int) {
        Int a = (Int)a_;
        if(b_ instanceof Int) {
            Int b = (Int)b_;
            return new Int(a.value + b.value);
        }
        else if(b_ instanceof Real) {
            Real b = (Real)b_;
            return new Real(a.value + b.value);
        }
    }
    else if(a_ instanceof Real) {
        Real a = (Real)a_;
        if(b_ instanceof Int) {
            Int b = (Int)b_;
            return new Real(a.value + b.value);
        }
        else if(b_ instanceof Real) {
            Real b = (Real)b_;
            return new Real(a.value + b.value);
        }
    }
    throw new IllegalArgumentException();
}
add :: Num -> Num -> Num
add (Int a)  (Int b)  = Int (a + b)
add (Int a)  (Real b) = Real (fromInteger a + b)
add (Real a) (Int b)  = Real (a + fromInteger b)
add (Real a) (Real b) = Real (a + b)
public static interface Bijection<A,B> {
    B forward(A v);
    A backward(B v);
}

Bijection<Integer,Integer> inc = 
new Bijection<Integer,Integer> {
   public Integer forward(Integer v) {
       return v + 1;
   }
   public Integer backward(Integer v) {
       return v - 1;
   }
}
data Bijection a b = 
    Bijection (a -> b) (b -> a)
    
forward (Bijection f _) v = f v 
backward (Bijection _ f) v = f v

inc = Bijection (\v -> v + 1) (\v -> v - 1)
In future:
data Bijection a b = Bijection {
    forward :: a -> b,
    backward :: b -> a
}

inc = Bijection {
    forward v = v + 1,
    backward v = v - 1
}

Longer examples

publid static int findIndex(List<String> strings, String target) {
    int low=0, high=xs.size();
    while(high-low > 1) {
        int middle = (low + high)/2;
        int cmp = xs.get(middle).compareTo(target);
        if(cmp < 0)
            low = middle;
        else if(cmp > 0)
            high = middle;
        else
            return middle;
    }
}
findIndex :: [String] -> String -> Integer
findIndex strings target = loop 0 (length strings) 
  where
    loop low high 
    | high - low > 1 = do
        middle = (low + high) `div` 2
        cmp = compare (xs!middle) target
        if cmp < 0
        then loop middle high
        else if cmp > 0
        then loop low middle
        else middle
    | otherwise = low