low-level approach

Dan Webb dwebb@dwebb.com
Tue Jan 15 08:47:02 2002


>For capturing vars in an environment, take
>(define (add n) (lambda (x) (+ n x)))
>(define add-5 (add 5))
>
>(In C you could model this as
>int add(int a, int b) { return a+b; }
>int add_5(int x) { return add(5,x); }
>which includes more parameter passing.)

But sometimes the values of a and b are available at different places in 
your program, in which case you have to curry the function.  Currying is 
easy in a language with a hierarchical lexical environment and higher 
order functions, but in C you'd have to fake out an environment by either 
using a global variable or by passing an environment to all your 
functions.  In C++ you could use a class to accomplish it (which gives 
you one more level of environment), which is far more tedious than in 
Lisp or Scheme.  Dozens of lines of code compared to less than one line 
of code.  Such a simple task being so difficult to accomplish means that 
it is seldom done, which results in less code reuse in C/C++ programs.

Dan Webb
dwebb@dwebb.com