SI 413 Fall 2023 / Homeworks


This is the archived website of SI 413 from the Fall 2023 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 8: Functions and arguments

1 Pass by what?

Here is a small SPL program with a single function that gets called twice.

new a := 20;
new b := 10

new foo := lambda x {
  x := x + x;
  ret := x * b;
}

write foo@a;
write a;
write foo@b;
write b;

Clearly four numbers will get printed by this piece of code. Tell me what those four numbers will be under:

  1. Pass by value
  2. Pass by reference
  3. Pass by value/result

2 Value/result example

In Java function parameters are passed by sharing by default. In class, we saw a different kind of parameter passing mode called pass by value/result. Write a small Java program demonstrating that function parameters in Java really are passed by sharing and not by value/result. That is, your program should do something different in each of these parameter passing modes.

As always, I want you to come up with your own examples! You can work together on homeworks, but the examples you turn in should be unique.

Clearly specify what the result of your function would be under pass by sharing and under pass by value/result.

3 Barbarian Sausage Gang

Consider the following Scheme program:

(define (f x y)
  (display x)
  (display 'a)
  (display y)
  x)

(define (g x y)
  (cond ((eqv? x 'g)
         (display x))
        ((eqv? x y)
         (display 'e))))

(g (f ?? ??) (f ?? ??))
  1. Fill in the ?? blanks in the function call so that the program prints barbarian under call-by-name evaluation rules.

  2. Fill in the ?? blanks in the function call so that the program prints sausage under applicative-order evaluation rules.

  3. Fill in the ?? blanks in the function call so that the program prints gang under normal-order evaluation rules.