Thursday, June 05, 2008

Anonymous Recursive Function

I thought I'd give the 99 Lisp Problems a spin as a way of exploring Pico Lisp. One of my first outings came up with the discovery that if you call a function without enough arguments the remaining arguments get set to NIL this can be used to do default arguments as follows:

(de fun (X Y)
(let (Y (or Y 5))
(+ x Y)))

(fun 1 2) -> 2

(fun 1) -> 6

I did this to simulate an accumulator argument in a recursive function. But then i got to thinking, I'm executing the let on each recursive call, which seems kind of a waste as it is only needed on the first call. Well Pico has an answer for this recur and recurse which allows you to define an anonymous recursive function. Now this is something you don't find in your garden variety common lisp!

a list reverse (yes I know its built in but it see the 99 Python problems above) is then implemented like this;

(de my-rev (lst)
(let (res ())
(recur (lst res)
(if (pair lst)
(recurse (cdr lst) (cons (car lst) res))

That is one neat way to make do without auxiliary functions.

Wednesday, June 04, 2008

Lisp is addictive

A week or so of Pico Lisp and I'm addicted again. Now the label was right, this is no common Lisp. Part of this means that a lot of built in functions have different names but then lets be honest, 'filter' is a much better name then 'remove-if-not'. I also found that there is no 'reduce' function but it was trivial to implement.

I have to say that having having one name space for functions and variables works, and using quote as lambda is growing on me. Pico lisp is proving to be a very succinct lisp to program in, which is a good thing.

Time permitting I'll be doing a lot more pico lisp hacking in the near future, port a few scripts I've done in python as practice and then on to some more interesting stuff. I've got some pointers on how to mix pico lisp and c code, which will is bound to prove useful

Sunday, June 01, 2008

Been away from Lisp too long.

Yes I'm sad to say that I've spent too long without messing about in Lisp. Now that I'm trying with pico lisp theres a lot of basic logic which seems to be bogging me down, such as when to quote and unquote things (though Pico lisp seems to be a little different in this regard).

Another omission, from the point of view of convenience, is the lack of a well developed string formatting function. Granted the built in symbol manipulation primitives are more general then a format function but in simple cases they lead to more verbose code. Note Pico Lisp does have a function called format but it is restricted to formating a single number as a fixed precision decimal.

Still I'm working slowly in adapting some examples from a Common Lisp book into Pico Lisp, and if nothing else its an enjoyable exercise.

It looks like I am on the Pico lisp mailing list, though I never got notified that my subscription request was accepted. Still I got to ask my question now, which is good.