SI 413 Fall 2021 / Homeworks


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

Homework 9: 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 Overloading and polymorphism

Answer the following questions to demonstrate your knowledge of these concepts.

  1. Why doesn’t Python support function overloading the way C++ and Java do?

  2. What would need to be added to the SPL language in order to make function overloading possible?

  3. What would need to be added to the SPL language in order to make subtype polymorphism possible?

  4. The following code works in C++ and prints “2”:

    bool x = true;
    cout << x + x << endl;

    But the C++ standard says that the + operator only works for numeric types, not for bools. What’s going on here?

4 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.