low-level approach

Ulrich Hobelmann u.hobelmann@tu-bs.de
Tue Jan 15 01:28:02 2002


Kyle Lahnakoski wrote:
> Maybe you would be so nice as to provide an example where the "powerful"
> aspect of first class functions shows itself by shortening code length
> or reducing development time.

Of course there's no objective measure of this, and maybe you can do
well with C and I just can't ;)
But then that article comparing Lisp to Java says Lisp is more
productive...
For instance take
(define (binary-op fun)
  (let* ((a (stack:pop))
	 (b (stack:pop)))
    (stack:push (fun a b))))
which is imaginary code for a stack machine. With it you can just
partially parametrize
the function to yield other functions, like
(define add (binary-op +))
(define mul (binary-op *))
The SICP book has more examples where real higher order-functions are
nice.
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.)

In C, where there is no lexical environment (unlike, say Pascal), you
have to explicitly
pass those values in a parameter or something like that, which is not
strictly less
powerful, but at least for hand-written programs (which C is mostly used
for) it is
annoying at best.

> I will grant that C is bug prone, but in the same note I am unsure how
> safe Lisp is.  It strikes me that Lisp lacks type information; I am able
> to pass anything I want to a Lisp function and it will not complain
> unless it runs out of symbols.  I could certainly be wrong here.

I feel the same. This is probably the coolest thing about ML/Haskell
dialects:
"a well-typed program cannot go wrong". This isn't so in Lisp or Java,
which have
no compile-time, but runtime typing. In this case I prefer static
languages.
It also makes compilation more efficient.

> > Also, in general, most Lisps have other types like vectors and records.
> 
> I have only seen these implemented using lists.  Maybe there are Lisp
> versions that implement these natively.

See the R5RS Scheme report on vectors.

> > Other Lambda-based languages like Haskell/ML use these other types much
> > more than lists...
> 
> So would you consider this a good thing?

To me it's much more readable (apart from compile-time type checking) to
have, say, a tree defined as abstract type than to have just lispy
constructor
and selector functions around...

> > You can even implement your own syntax macros.
> 
> How would you say these macros are better than including a compiler
> library to C, if at all?

Maybe not, but then Lisp has more uniform syntax, which makes syntax
extensions
easier. But then AFAIK C++ allows at least operator overloading.
It C would be more like Haskell in allowing definitions, it would be
better!
I haven't tried out any compiler library stuff yet...

> I would love to deliver examples of Lisp and C to show they are very
> similar, but any example I come up will be disputed as contrived.  An
> example can only prove my premise false.  Unfortunately I can not think
> of one because I believe an example does not exist.

Right. Likewise the above Scheme code is contrived...
But again I feel there's more freedom to write anything I want, unlike
C...

Best wishes, Ulrich