1 <?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>Lists</title><link type="text/css" rel="stylesheet" href="wikistyle.css"/></head><body>
\r
3 <h2 id="Comparison_of_Java_and_SCL">Comparison of Java and SCL</h2>
\r
5 <h3>Language constructions and patterns</h3>
\r
9 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>
\r
10 <td><pre>0.5 + x * (2e-3 + x * 2)</pre></td>
\r
14 <td><pre>x >= 0 && x <= 1</pre></td>
\r
15 <td><pre>x >= 0 && x <= 1</pre></td>
\r
19 <td><pre>Math.sin(a1) - Math.cos(a2)</pre></td>
\r
20 <td><pre>sin a1 - cos a2</pre></td></tr>
\r
23 <td><pre>cond ? value : alternative</pre></td>
\r
24 <td><pre><b>if</b> cond <b>then</b> value <b>else</b> alternative</pre></td>
\r
28 <td><pre>"I am " + age + " years old."</pre></td>
\r
30 <pre>"I am " + show age + " years old."</pre>
\r
34 <pre>"I am ${age} years old."</pre>
\r
39 <b>final</b> int sum = a + b;</pre>
\r
45 public static String decorate(String x) {
\r
46 return "(" + x + ")";
\r
49 decorate :: String -> String
\r
50 decorate x = "(" + x + ")"</pre>
\r
51 (the first line is optional)
\r
70 <b>else if</b>(cond2)
\r
74 </pre></td><td><pre>
\r
76 <b>then</b> statement1
\r
77 <b>else if</b> cond2
\r
78 <b>then</b> statement2
\r
79 <b>else</b> statement3
\r
85 </pre></td><td><pre>
\r
92 <b>while</b>(cond) statement;
\r
93 </pre></td><td><pre>
\r
94 while (\() -> cond) (\() -> statement)
\r
96 In future with improvements to macros:
\r
98 while cond statement
\r
104 <b>switch</b>(value) {
\r
105 <b>case</b> Label1:
\r
108 <b>case</b> Label2:
\r
115 var = <b>match</b> value <b>with</b>
\r
125 <b>return</b> x; // In the middle of the code
\r
126 </pre></td><td>No exact correspondence. However, every control sequence can be written in the following form:
\r
127 <pre>label1 initialState
\r
131 then label2 modifiedState1
\r
132 else label3 modifiedState2
\r
137 label1 modifiedState</pre>
\r
152 (a, b) = <b>if</b> cond
\r
153 <b>then</b> (aExp1, bExp1)
\r
154 <b>else</b> (aExp2, bExp2)
\r
171 The name of the function <code>getRef</code> will change in the future and is
\r
172 probably replaced by some special syntax. For example in ML <code>getRef a</code>
\r
173 would be written as <code>!a</code>.
\r
178 public static String f(int x) {
\r
180 return Integer.toString(x);
\r
185 f :: Integer -> Maybe String
\r
186 f x | x >= 0 = Just (show x)
\r
187 | otherwise = Nothing
\r
192 public static int invF(String s) {
\r
196 return Integer.parseInt(s);
\r
199 invF :: Maybe String -> Integer
\r
201 invF (Just s) = read s
\r
206 Arrays.asList(new String[] {"x", "y", "z"})
\r
222 List<String> decorated = new ArrayList<String>();
\r
223 for(String s : undecorated)
\r
224 decorated.add("(" + s + ")");
\r
227 decorated = ["(" + s + ")" | s <- undecorated]
\r
231 decorated = map (\s -> "(" + s + ")") undecorated
\r
236 List<String> nonempty = new ArrayList<String>();
\r
242 decorated = [s | s <- l, s != ""]
\r
246 decorated = filter (\s -> s != "") l
\r
251 List<List<String>> listOfLists;
\r
252 List<String> flattened
\r
253 = new ArrayList<String>();
\r
254 for(List<String> l : listOfLists)
\r
259 flattened = [s | l <- listOfLists, s <- l]
\r
263 flattened = sum listOfLists
\r
267 flattened = join listOfLists
\r
272 throw new RuntimeException("Unhandled case.");
\r
275 <pre>fail "Unhandled case."</pre>
\r
282 <b>throw</b> new ExceptionX(...);
\r
284 } <b>catch</b>(ExceptionX e) {
\r
291 Currently no correspondence. Exceptions need to be thrown and catched in Java code.
\r
295 <b>synchronized</b>(obj) {
\r
300 Currently no correspondence. Synchronization must be done in Java code.
\r
304 <b>final static class</b> Vec3 {
\r
305 public final double x;
\r
306 public final double y;
\r
307 public final double z;
\r
309 public Vec3(double x,
\r
319 <b>data</b> Vec3 = Vec3 Double Double Double
\r
325 <b>data</b> Vec3 = Vec3 {
\r
334 public static interface Num {}
\r
335 public static class Int implements Num {
\r
336 public static final int value;
\r
337 public Int(int value) {
\r
338 this.value = value;
\r
341 public static class Real implements Num {
\r
342 public static final double value;
\r
343 public Real(double value) {
\r
344 this.value = value;
\r
349 data Num = Int Integer
\r
354 public static Num add(Num a_, Num b_) {
\r
355 if(a_ instanceof Int) {
\r
357 if(b_ instanceof Int) {
\r
359 return new Int(a.value + b.value);
\r
361 else if(b_ instanceof Real) {
\r
363 return new Real(a.value + b.value);
\r
366 else if(a_ instanceof Real) {
\r
368 if(b_ instanceof Int) {
\r
370 return new Real(a.value + b.value);
\r
372 else if(b_ instanceof Real) {
\r
374 return new Real(a.value + b.value);
\r
377 throw new IllegalArgumentException();
\r
381 add :: Num -> Num -> Num
\r
382 add (Int a) (Int b) = Int (a + b)
\r
383 add (Int a) (Real b) = Real (fromInteger a + b)
\r
384 add (Real a) (Int b) = Real (a + fromInteger b)
\r
385 add (Real a) (Real b) = Real (a + b)
\r
389 public static interface Bijection<A,B> {
\r
394 Bijection<Integer,Integer> inc =
\r
395 new Bijection<Integer,Integer> {
\r
396 public Integer forward(Integer v) {
\r
399 public Integer backward(Integer v) {
\r
405 data Bijection a b =
\r
406 Bijection (a -> b) (b -> a)
\r
408 forward (Bijection f _) v = f v
\r
409 backward (Bijection _ f) v = f v
\r
411 inc = Bijection (\v -> v + 1) (\v -> v - 1)
\r
417 data Bijection a b = Bijection {
\r
430 <h3>Longer examples</h3>
\r
434 publid static int findIndex(List<String> strings, String target) {
\r
435 int low=0, high=xs.size();
\r
436 while(high-low > 1) {
\r
437 int middle = (low + high)/2;
\r
438 int cmp = xs.get(middle).compareTo(target);
\r
441 else if(cmp > 0)
\r
449 findIndex :: [String] -> String -> Integer
\r
450 findIndex strings target = loop 0 (length strings)
\r
453 | high - low > 1 = do
\r
454 middle = (low + high) `div` 2
\r
455 cmp = compare (xs!middle) target
\r
457 then loop middle high
\r
459 then loop low middle
\r