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