[Haskell-cafe] some ideas for Haskell', from Python

Tim Wawrzynczak inforichland at gmail.com
Wed Jan 14 14:30:28 EST 2009

On Wed, Jan 14, 2009 at 1:14 PM, Jonathan Cast <jonathanccast at fastmail.fm>wrote:

> On Wed, 2009-01-14 at 11:06 -0800, Max Rabkin wrote:
> > On Wed, Jan 14, 2009 at 10:48 AM, Jonathan Cast
> > <jonathanccast at fastmail.fm> wrote:
> > > Do you have an example of
> > > a macro that can't be replaced by higher-order functions and laziness?
> >
> > I believe I do: one macro I found useful when writing a web app in
> > Lisp was something I called hash-bind, which binds variables to the
> > values in a hashtable, with the variable names as keys. For example:
> >
> > (hash-bind (a b) hashtable body)
> > ==
> > (let
> >     ((a (lookup hashtable "a"))
> >      (b (lookup hashtable "b"))
> >     body)
> >
> > I found this very useful in places where I was given URL request
> > parameters in a hashtable and wanted to extract some variables from
> > it. I don't believe it can be replaced by a higher order function
> > (though I may be wrong).
> Thanks!  When you *know* there's a good reason people say something, and
> can't find a good example of *why*, it's a tremendous relief when when
> you find one.  Sort of restores your faith in humanity :)
> jcc
I thought of another good case (Shamelessly stolen from Paul Graham's 'On
Lisp').  When defining a function to average the results of the list, you
could define avg like this:

(defun avg (&rest args)
  (/ (apply #'+ args) (length args)))

Or as a macro like this:

(defmacro avg (&rest args)
  `(/ (+ , at args) ,(length args)))

The reason the macro is better is that the length of the list is known at
compile time, so you don't need to traverse the list to calculate the length
of the list.

Food for thought, anyway.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090114/190b33fb/attachment.htm

More information about the Haskell-Cafe mailing list