Principles of Programming Languages — Homework 9

Due by class time Thursday, October 10

Reading

Review today's code examples from class.

Exercises

Download the file assign9.scm and use it as your starting point for this assignment (this is our interpreter from class today).

  1. Add a new primitive function to the interpreter's initial environment called (cube n), which computes the cube of its argument. Examples:

      (meaning '(cube 3) ) → 27
    
      (meaning '(cube (1 + 2)) ) → 27
    
      (meaning '(cube (square b)) ) → 64
    
  2. Currently, application expressions must be of the form (<var> <exp>), that is, the application operator <var> must be a symbol like square, minus, etc. Modify the interpreter so that the application operator can be any expression in our language, as shown in the examples below:

      (meaning '((if True then square else squareroot) 4) ) → 16
    
      (meaning '((if False then square else squareroot) 4) ) → 2
    
      (meaning '((if ((1 + 1) == 2) then ! else square) (2 + 3)) ) → 120
    
      (meaning '((if False then ! else (if ((1 + 1) == 3) then ! else square)) 5) ) → 25
    
  3. Add support to the interpreter for application expressions of the form (<var> <exp> <exp>) containing two operands. Also add a new primitive function called (average x y) to the initial environment, which takes two input values and computes their average. Hint: you will need to add a new 2-argument lambda function to the initial environment, bound to the symbol average. For example:

      (meaning '(average 3 5) ) → 4
    
      (meaning '(average a c) ) → 2
    
      (meaning '(average (a + b) (c + 4)) ) → 5
    

Turning in Your Homework