Favorite "underrated" Lisp feature?

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

Re: Favorite "underrated" Lisp feature?

Post by findinglisp » Fri Aug 08, 2008 9:08 am

tayssir wrote: To be cute, I responded with:

Code: Select all

(defmethod fib ((n (eql 0)))
  0)
(defmethod fib ((n (eql 1)))
  1)
(defmethod fib (n)
  (+ (fib (- n 1))
     (fib (- n 2))))
(Is this a silly trick? Well, maybe the whole example is silly -- people who actually need the Fibonacci numbers probably use a different algorithm, beyond even caching. ;) )
IMO, that specific case is a bit of a silly trick. In other words, I wouldn't write FIB that way, even if I used that simple algorithm, mostly because you have to have all the three pieces of it to work correctly and so if they all have to be defined together, you might as well put them in a single definition with a COND form. But, the silly trick does make the point quite well. 8-)

Where I find that the GF dispatch really helpful is where I want to effectively expand a dispatch table at runtime. Say you're writing a network analyzer that decodes packets. If you want to be able to have a modular system, where you can load new functions that decode different packet types, you'd like to avoid having all the logic that figures out the packet type and then hands the packet to the decode centralized. If you do that, you either end up editing the dispatch function every time you add a new decoder, or you end up designing a dynamic dispatching scheme using something like a hash table. That last one works, and is relatively easy to do, but it's so much easier to just define a generic function and have CLOS figure it all out. And optimize it all for high performance, for that matter.

Peter Seibel used this technique to great effect in his binary decoder routines in Practical Common Lisp. I was reading through those chapters when I had that "aha!" moment.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

schoppenhauer
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany
Contact:

Re: Favorite "underrated" Lisp feature?

Post by schoppenhauer » Fri Aug 08, 2008 2:21 pm

findinglisp wrote:
schoppenhauer wrote:Compiler Macros
Interesting. Care to say why? I have never really had an occasion to use a compiler macro. Regular macros, yes, all the time. Compiler macros, never.
As Geoff Wozniak already said, compiler macros are a good thing when needed. I actually never needed them until a few days ago (and I didnt even write the first I used myself - the situation where I needed them is complicated, dont want to explain that now). I thought, "underrated" means something like "not widely used", "not often on topic". And thats what I think compiler macros are.

I think compiler macros are something, other Languages could benefit from. Compilers for other Languages are using Heuristics for optimizing, they are replacing code that will always have a constant value by the value, "inlining" small functions that are not declared externally, removing unused variables (unless declared volatile, etc.), etc. - they do anything that can be done at the moment to "guess" whats best for optimizing your code, and so do all good CL-Compilers - I even read somewhere (dont know where) that somebody wants to attatch Artificial Intelligence to the GNU Compiler Collection for optimizing. But there will certainly always, or at least for a long time, be Situations where it isnt clear to the compiler, and where the Compiler cannot guess that some value is easier to calculate in special cases which are known before compiling, but which are not "obvious". And with compiler macros, you can tell the Compiler what to do, if it cannot know exactly itself. There may only be few situations where this is useful, but then it is a good feature.
Sorry for my bad english.
Visit my blog http://blog.uxul.de/

dlweinreb
Posts: 41
Joined: Tue Jul 01, 2008 5:11 am
Location: Lexington, MA
Contact:

Re: Favorite "underrated" Lisp feature?

Post by dlweinreb » Sat Aug 09, 2008 8:27 pm

The compiler macro feature is definitely an advanced feature. I'm not even entirely sure I've ever written one myself. Here's an example. If I'm not mistaken, the idea here is to optimize out the need for the "if" at runtime, in a common case. It's not at all clear that this optimization is tremendously important, and the amount of programmer time needed to make quite sure that the define-compiler-macro form is bug-free may well be more than the total amount of speedup that this will ever provide anywhere.

Code: Select all

(defun curry (function &rest args)

  "Return a function of any number of arguments, which, when
   called, calls the given function on the given args followed
   by all those arguments."

  (if (and args (null (cdr args)))      ;fast test for length = 1
    (let ((arg (car args)))
      #'(lambda (&rest more-args)
          (apply function arg more-args)))
    #'(lambda (&rest more-args)
	(apply function (append args more-args)))))

(define-compiler-macro curry (&whole form function &rest args &environment env)

  "Return a function of any number of arguments, which, when
   called, calls the given function on the given args followed
   by all those arguments."

  (declare (ignore env))
  (if (and (listp function)
	   (eq (first function) 'function)
	   (symbolp (second function))
	   (and args (null (cdr args))))
    `#'(lambda (&rest more-args)
	 (apply ,function ,(car args) more-args))
    form))

Kompottkin
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany
Contact:

Re: Favorite "underrated" Lisp feature?

Post by Kompottkin » Sun Aug 10, 2008 3:31 am

My favourite, ridiculously underappreciated feature of Common Lisp? Its community.

Post Reply