Higher Order Perl

Discussion of books and various other resources about Lisp
Post Reply
theclapp
Posts: 17
Joined: Wed Jul 16, 2008 11:05 am

Higher Order Perl

Post by theclapp » Mon Oct 06, 2008 1:55 pm

Many people say that learning Lisp changes the way they program in other languages. Well, I suspect that reading Higher Order Perl will change the way I program in Lisp, so I thought it deserved at least a tip of the hat here.

Disclaimer: It's entirely possible that everything in HOP is already in SICP. But I haven't read SICP. One day ... :)

My favorite chapter so far (haven't finished it yet) was on iterators. I'm familiar with recursion, closures, caching, and memoization, so iterators were the first really new stuff for me, and I quite like them.

One interesting thing is that it mentions the problem of having a predicate that can validly return NIL (or undef in Perl-speak) but still mean "true", and that Lispers call this the "semi-predicate problem". I've read several Lisp books and never run across the term "semi-predicate problem". (Google shows it's fairly common, though. Perhaps if I'd ever taken an actual class on Lisp/Scheme I'd've heard of it before. Or maybe I just haven't read the right books. :) )

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: Higher Order Perl

Post by findinglisp » Mon Oct 06, 2008 3:43 pm

theclapp wrote: One interesting thing is that it mentions the problem of having a predicate that can validly return NIL (or undef in Perl-speak) but still mean "true", and that Lispers call this the "semi-predicate problem". I've read several Lisp books and never run across the term "semi-predicate problem". (Google shows it's fairly common, though. Perhaps if I'd ever taken an actual class on Lisp/Scheme I'd've heard of it before. Or maybe I just haven't read the right books. :) )
Actually, I think Lisp helps solve the semi-predicate problem in many cases. Two examples:
  1. First, by having dynamic typing, you can return anything from a function. Thus, if a function normally returns an integer, you don't need a special sentinel value drawn from the set of integers to represent an error, you can simply use NIL or another symbol instead. See what CL does with things like READ-CHAR for instance.
  2. Second, CL has provisions for multiple value return. The hash lookup functions use that to signal the difference between "not present" and "the value associated with this key really is NIL." Hash tables are a good place to see the semi-predicate problem in action because if you build them right, you can stuff anything into them, leading to the issue of what you return when the key really isn't present. In C, you'd do the same thing, but you'd pass in pointers to boolean values that are updated by the code and then read by the caller. Lisp makes this a bit more elegant.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

theclapp
Posts: 17
Joined: Wed Jul 16, 2008 11:05 am

Re: Higher Order Perl

Post by theclapp » Mon Oct 06, 2008 4:16 pm

findinglisp wrote:Actually, I think Lisp helps solve the semi-predicate problem in many cases. Two examples:
[...]
I agree with all of that, and so does MJD (the author), who mentions several ways around it, including returning multiple values, like gethash does. I just wasn't familiar with the term "semi-predicate", and it struck me funny that this Perl guy is telling me it's a term from Lisp.

Wodin
Posts: 56
Joined: Sun Jun 29, 2008 8:16 am

Re: Higher Order Perl

Post by Wodin » Tue Oct 07, 2008 12:24 am

theclapp wrote:One interesting thing is that it mentions the problem of having a predicate that can validly return NIL (or undef in Perl-speak) but still mean "true", and that Lispers call this the "semi-predicate problem". I've read several Lisp books and never run across the term "semi-predicate problem". (Google shows it's fairly common, though. Perhaps if I'd ever taken an actual class on Lisp/Scheme I'd've heard of it before. Or maybe I just haven't read the right books. :) )
I believe I've seen mention of it in PAIP.

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: Higher Order Perl

Post by findinglisp » Tue Oct 07, 2008 9:22 am

theclapp wrote:
findinglisp wrote:Actually, I think Lisp helps solve the semi-predicate problem in many cases. Two examples:
[...]
I agree with all of that, and so does MJD (the author), who mentions several ways around it, including returning multiple values, like gethash does. I just wasn't familiar with the term "semi-predicate", and it struck me funny that this Perl guy is telling me it's a term from Lisp.
Gotcha.

One of the things that struck me most about learning Lisp was how often the semi-predicate problem goes away. In C and even Java, I was always bumping into it. With Lisp, I kept doing something like, "Hmmm... all integers are valid return values here. I guess I'll just return NIL if I didn't find whatever I was looking for in the data structure. Wow! That's so cool!" :)
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Post Reply