20-CS-4003-001 Organization of Programming Languages Fall 2017
Arrays and Lists

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures

Head, tail, and random element of a list

Java:
   
   public class intro_2 {
      public static void main (String args[]) {
         Vector  v = new Vector  ();
         v.add(1); v.add(2); v.add(11); v.add(8); v.add(3);
         v.add(9); v.add(10);
         int p1 = v.elementAt(0);
         System.out.println(p1);
         Vector  p2 = (Vector )v.clone();
         p2.remove(0);
         System.out.println(p2.toString());
         int p3 = v.elementAt(3);
         System.out.println(p3);
         Vector  p4 = new Vector  ();
         p4.add(1); p4.add(2);
         Vector  x = new Vector  ();
         x.add(3); x.add(4); x.add(5);
         p4.addAll(x);
         System.out.println(p4.toString());
      }
   }

The type of p1 must be declared explicitly as an array of int values.

 
Haskell:
   ghci> let p1 = head [1, 2, 11, 8, 3, 9, 10]
   ghci> let p2 = tail [1, 2, 11, 8, 3, 9, 10]
   ghci> let p3 = [1, 2, 11, 8, 3, 9, 10] !! 3
   ghci> let p4 = [1, 2] ++ [3, 4, 5]
   ghci> p1
   1
   ghci> p2
   [2,11,8,3,9,10]
   ghci> p3
   8
   ghci> p4
   [1,2,3,4,5]
 
Scheme:
   (let* ((v '(1 2 11 8 3 9 10))
          (p1 (car v))
          (p2 (cdr v))
          (p3 (car (cdr (cdr (cdr v)))))
          (p4 (append '(1 2) '(3 4 5))))
      (display p1)(display " ")
      (display p2)(display " ")
      (display p3)(display " ")
      (display p4)(display " ")
      #t)