20-CS-4003-001 Organization of Programming Languages Fall 2017
Selection

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures

if-then-else and switch-case

Scheme:

  (define mult
    (lambda (l)
      (if (null? l)
          1
          (* (car l) (mult (cdr l))))))

  (mult '(5 4 6 7 0 6 7))

Haskell:

  mult l = 
    if (l == []) then 1
    else (head l)*(mult (drop 1 l))

Java:

  import java.util.*;

  class M {  
     public int mult (Vector  v) {  
        if (v.isEmpty()) return 1;
        int n = v.get(0);
        v.removeElementAt(0);
        return n*mult(v);
     }
  }

  public class a {
     public static void main (String args[]) {
        Vector  v = new Vector  ();
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        v.add(5);
        System.out.println((new M()).mult(v));
     }
  }
 -  The principle means of selection is the if-then-else construct. A simple example is shown for three languages here. switch-case is also available (as cond in Scheme) but not shown.