Re: Simple recursive functions in Lisp
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Fri, 09 Feb 2007 21:00:27 +0100
Frode Vatvedt Fjeld <frodef@xxxxxxxxx> writes:
Frode Vatvedt Fjeld <frodef@xxxxxxxxx> writes:
[..] But here I understood the context to be
programmers working with (writing, reading, debugging..) code. And
then I think it makes perfect sense to say e.g that
(defun sum (list)
(loop for x in list sum x))
is vastly better than
(defun sum (list)
(if (null list)
0
(+ (car list)
(sum (cdr list)))))
Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx> writes:
Of course, since LOOP is a higher level abstraction. You should
compare with this iterative form:
(defun sum (list)
(do ((current list (cdr current))
(sum 0))
((null current) sum)
(setf sum (+ sum (car current)))))
Why should I, when I'd never ever write something like that in
practice? Why is loop disqualified as "higher level"? (And why stop at
'do', when tagbody/go will do the job too?) I'm very grateful that
loop exists so I don't have to brain-parse the unreasonably cryptic
'do' forms. [...]
If you want to compare with LOOP,
(defun sum (list)
(loop :for x :in list :sum x))
use a macro such as RECURSE that has the same knowledge of lists and
sums, to let you write:
(defun sum (list)
(recurse :for x :in list :sum x))
(defmacro recurse (&key for in sum)
(let ((fname (gensym))
(vlist (gensym))
(vresu (gensym)))
`(labels ((,fname (,vlist ,vresu)
(let ((,for (car ,vlist)))
(if (null ,vlist)
,vresu
(+ ,sum (,fname (cdr ,vlist)))))))
(,fname ,in 0))))
I don't really know about everyone else, but personally I "prove"
functions about once every odd leap year. I believe that the overall
read/writeability of the code to be of more value.
Of course. That's why we're using Common Lisp, not scheme or haskell...
--
__Pascal Bourguignon__ http://www.informatimago.com/
NEW GRAND UNIFIED THEORY DISCLAIMER: The manufacturer may
technically be entitled to claim that this product is
ten-dimensional. However, the consumer is reminded that this
confers no legal rights above and beyond those applicable to
three-dimensional objects, since the seven new dimensions are
"rolled up" into such a small "area" that they cannot be
detected.
.
- References:
- Simple recursive functions in Lisp
- From: S. Robert James
- Re: Simple recursive functions in Lisp
- From: Kaz Kylheku
- Re: Simple recursive functions in Lisp
- From: S. Robert James
- Re: Simple recursive functions in Lisp
- From: Frode Vatvedt Fjeld
- Re: Simple recursive functions in Lisp
- From: Joe Marshall
- Re: Simple recursive functions in Lisp
- From: Frode Vatvedt Fjeld
- Re: Simple recursive functions in Lisp
- From: Pascal Bourguignon
- Re: Simple recursive functions in Lisp
- From: Frode Vatvedt Fjeld
- Simple recursive functions in Lisp
- Prev by Date: Re: Exported closure issue
- Next by Date: Re: Lambda-calculus and lisp
- Previous by thread: Re: Simple recursive functions in Lisp
- Next by thread: Re: Simple recursive functions in Lisp
- Index(es):
Relevant Pages
|