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