Tuesday, July 25, 2006

a lisp evangelist who actually makes sense

In my continuing quest to learn and experiment with F#/OCaml, I decided that I should get to know tried-and-true functional programming techniques in a functional programming mindset. Otherwise, I would end up writing the same code but in different syntax, mmm'kay? I remember reading Perl gurus mocking people who programmed "C in Perl", and more recently I remember a case of a Python guru complaining about some "Java in Python". I've certainly run across OCaml examples that were clearly imperative-style, not that there's anything wrong with that.

Anyway, in my attempt to figure out how to do "functional design" I came up with this link. I highly recommend it. The writer tries to get across exactly what makes Lisp special and, as he describes, enlightening. But he does it by starting with concepts that are understood by his audience, much like ocaml-tutorial. He starts with XML, an extendable tree data format. From there he goes to Ant, in which XML is both data and code. As he says, the jungle of parantheses in Lisp is just a more concise way than XML of expressing a collection of nested elements, so it's not that bad to convert an XML document to a Lisp s-expression. Then he points out that Lisp can assign identifiers, known as symbols, to lists. Lisp functions are created just by passing a couple lists representing parameters and code to a built-in function that creates a function from the lists. Finally, lists are evaluated as functions, in which the first element is the function name and the remaining elements are arguments, unless the list is marked as being data with a simple ' mark in front. With these pieces, he can explain that a Lisp macro is just a special function that takes a data list and returns a (nested) list that will be evaluated as code. Unlike C with its preprocessor or Java using Ant, Lisp needs no templating language because it is its own!
Moreover, defining new macros is painless enough that Lisp can be extended at will...in fact, one way of attacking a problem is to define some macros in what amounts to a "domain-specific language" that is in turn defined in terms of Lisp. Much different from what I've read about caml4p. Then again, I know that I have only a superficial understanding.

There's another article at the same site that explains the benefits of lazy evaluation and continuations. But learning Haskell is not something I wish to do right now...

No comments:

Post a Comment