20-CS-4003-001 Organization of Programming Languages Fall 2017
Expression Evaluation

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures

References and Values

Scheme:

   (let ((a (list 1)) (b (list (- 2 1))))
      (list (eq? a b) (equal? a b)))

Haskell:

   [1] == [2-1]
   "abc" == 'a':"bc"

Java:

   public class a {
      public static void main (String args[]) {
         String a = new String("Hello");
         String b = new String("Hello");
         System.out.println((a == b)+" "+
                             a.equals(b));
      }
   }

   public class a {
      public static void main (String args[]) {
         String a = "Hello";
         String b = "Hello";
         System.out.println((a == b)+" "+
                             a.equals(b));
      }
   }

   import java.util.*;

   public class a {
      public static void main (String args[]) {
         Vector <Integer> h = new Vector <Integer> ();
         h.add(10);
         System.out.print(h+" ");
         int a = h.get(0);
         System.out.println(a);
      }
   }
 -  Scheme objects are named references to data or procedures. The code to the left declares a and b and compares them. The value of both a and b is '(1). Yet (eq? a b) evaluates to #f because it tests equality of the reference whereas (equal? a b) evaluates to #t because it tests equality of the values of the references.

In Haskell data objects have values and comparison is on the values only. The statements to the left evaluate to True.

In Java, references are extremely important and all objects derived from classes are references. The Java code at the top left shows this: the result of execution is false true. Observe the equals method of the String class was necessary to compare the values of the objects a and b. However, in the case of primitive types objects are values only. The second section of code to the left shows this: the output is true true. But this makes it difficult to put primitive types into a container (which is expecting references). In fact, the third section of code shows a Vector object v, which is declared to store Integer objects, actually storing ints. The Java compiler makes the conversion automatically to Integer (which was not the case originally) and back again to get a. The output is [10] 10.