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. )
Higher Order Perl
-
- Posts: 447
- Joined: Sat Jun 28, 2008 7:49 am
- Location: Austin, TX
- Contact:
Re: Higher Order Perl
Actually, I think Lisp helps solve the semi-predicate problem in many cases. Two examples: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. )
- 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.
- 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/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Re: Higher Order Perl
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.findinglisp wrote:Actually, I think Lisp helps solve the semi-predicate problem in many cases. Two examples:
[...]
Re: Higher Order Perl
I believe I've seen mention of it in PAIP.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. :) )
-
- Posts: 447
- Joined: Sat Jun 28, 2008 7:49 am
- Location: Austin, TX
- Contact:
Re: Higher Order Perl
Gotcha.theclapp wrote: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.findinglisp wrote:Actually, I think Lisp helps solve the semi-predicate problem in many cases. Two examples:
[...]
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/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/