Class 21: More on Functions: Macros, Lazy Evaluation, Built-ins, and Operators

Slides

Display version (pdf).

Downloads

Homework

Reading (for next class): Section 6.1.2. Everything form "Boxing" (p. 228) on is optional.

Exercises

You can invoke the C preprocessor on any file! For example, if we have an SPL program stored in a.spl, with some C preprocessor commands, we can run

gcc -E - < a.spl
and the preprocessed code will be written to standard out.

The following is an SPL program to compute the greatest common divisor of two integers.

new mod := lambda a {                                              
  ret := lambda b {
    ret := a - (a/b)*b;
  };
};

new gcd := lambda a {
  ret := lambda b {
    if (b = 0) { ret := a; }
    else { ret := gcd(b)( mod(a)(b) ); }
  };
};

write gcd(24)(42);
  1. Re-write the curried function mod above as a function-like macro to do the same thing.
  2. Why couldn't we re-write the gcd function itself as a function-like macro?