1 == Comparison of Java and SCL ==
3 === Language constructions and patterns ===
7 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>
8 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>
12 <td><pre>x >= 0 && x <= 1</pre></td>
13 <td><pre>x >= 0 && x <= 1</pre></td>
17 <td><pre>Math.sin(a1) - Math.cos(a2)</pre></td>
18 <td><pre>sin a1 - cos a2</pre></td></tr>
21 <td><pre>cond ? value : alternative</pre></td>
22 <td><pre><b>if</b> cond <b>then</b> value <b>else</b> alternative</pre></td>
26 <td><pre>"I am " + age + " years old."</pre></td>
28 <pre>"I am " + show age + " years old."</pre>
32 <pre>"I am ${age} years old."</pre>
37 <b>final</b> int sum = a + b;</pre>
43 public static String decorate(String x) {
47 decorate :: String -> String
48 decorate x = "(" + x + ")"</pre>
49 (the first line is optional)
74 <b>then</b> statement1
76 <b>then</b> statement2
77 <b>else</b> statement3
90 <b>while</b>(cond) statement;
92 while (\() -> cond) (\() -> statement)
94 In future with improvements to macros:
102 <b>switch</b>(value) {
113 var = <b>match</b> value <b>with</b>
123 <b>return</b> x; // In the middle of the code
124 </pre></td><td>No exact correspondence. However, every control sequence can be written in the following form:
125 <pre>label1 initialState
129 then label2 modifiedState1
130 else label3 modifiedState2
135 label1 modifiedState</pre>
150 (a, b) = <b>if</b> cond
151 <b>then</b> (aExp1, bExp1)
152 <b>else</b> (aExp2, bExp2)
169 The name of the function <code>getRef</code> will change in the future and is
170 probably replaced by some special syntax. For example in ML <code>getRef a</code>
171 would be written as <code>!a</code>.
176 public static String f(int x) {
178 return Integer.toString(x);
183 f :: Integer -> Maybe String
184 f x | x >= 0 = Just (show x)
185 | otherwise = Nothing
190 public static int invF(String s) {
194 return Integer.parseInt(s);
197 invF :: Maybe String -> Integer
199 invF (Just s) = read s
204 Arrays.asList(new String[] {"x", "y", "z"})
220 List<String> decorated = new ArrayList<String>();
221 for(String s : undecorated)
222 decorated.add("(" + s + ")");
225 decorated = [ "(" + s + ")" | s <- undecorated ]
229 decorated = map (\s -> "(" + s + ")") undecorated
234 List<String> nonempty = new ArrayList<String>();
240 decorated = [ s | s <- l, s != "" ]
244 decorated = filter (\s -> s != "") l
249 List<List<String>> listOfLists;
250 List<String> flattened
251 = new ArrayList<String>();
252 for(List<String> l : listOfLists)
257 flattened = [ s | l <- listOfLists, s <- l ]
261 flattened = sum listOfLists
265 flattened = join listOfLists
270 throw new RuntimeException("Unhandled case.");
273 <pre>fail "Unhandled case."</pre>
280 <b>throw</b> new ExceptionX(...);
282 } <b>catch</b>(ExceptionX e) {
289 Currently no correspondence. Exceptions need to be thrown and catched in Java code.
293 <b>synchronized</b>(obj) {
298 Currently no correspondence. Synchronization must be done in Java code.
302 <b>final static class</b> Vec3 {
303 public final double x;
304 public final double y;
305 public final double z;
307 public Vec3(double x,
317 <b>data</b> Vec3 = Vec3 Double Double Double
323 <b>data</b> Vec3 = Vec3 {
332 public static interface Num {}
333 public static class Int implements Num {
334 public static final int value;
335 public Int(int value) {
339 public static class Real implements Num {
340 public static final double value;
341 public Real(double value) {
347 data Num = Int Integer
352 public static Num add(Num a_, Num b_) {
353 if(a_ instanceof Int) {
355 if(b_ instanceof Int) {
357 return new Int(a.value + b.value);
359 else if(b_ instanceof Real) {
361 return new Real(a.value + b.value);
364 else if(a_ instanceof Real) {
366 if(b_ instanceof Int) {
368 return new Real(a.value + b.value);
370 else if(b_ instanceof Real) {
372 return new Real(a.value + b.value);
375 throw new IllegalArgumentException();
379 add :: Num -> Num -> Num
380 add (Int a) (Int b) = Int (a + b)
381 add (Int a) (Real b) = Real (fromInteger a + b)
382 add (Real a) (Int b) = Real (a + fromInteger b)
383 add (Real a) (Real b) = Real (a + b)
387 public static interface Bijection<A,B> {
392 Bijection<Integer,Integer> inc =
393 new Bijection<Integer,Integer> {
394 public Integer forward(Integer v) {
397 public Integer backward(Integer v) {
404 Bijection (a -> b) (b -> a)
406 forward (Bijection f _) v = f v
407 backward (Bijection _ f) v = f v
409 inc = Bijection (\v -> v + 1) (\v -> v - 1)
415 data Bijection a b = Bijection {
428 === Longer examples ===
432 publid static int findIndex(List<String> strings, String target) {
433 int low=0, high=xs.size();
434 while(high-low > 1) {
435 int middle = (low + high)/2;
436 int cmp = xs.get(middle).compareTo(target);
447 findIndex :: [ String ] -> String -> Integer
448 findIndex strings target = loop 0 (length strings)
451 | high - low > 1 = do
452 middle = (low + high) `div` 2
453 cmp = compare (xs!middle) target
455 then loop middle high