Re: Simple recursive functions in Lisp



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.
.



Relevant Pages

  • Re: abundance of irrationals!)
    ... No such "sum" exists except as, and only as, the limit of the infinite ... If an infinite sequence does not have to "achieve" its limit, ... > It is only for lists not initially known not to be surjective that ...
    (sci.math)
  • Re: abundance of irrationals!)
    ... Not in that sum which stretches over all n. ... It is only for lists not initially known not to be surjective that ... "infinite" sequence. ... you cannot enumerate by them. ...
    (sci.math)
  • Re: Is there a polynomial algorithm for that problem?
    ... The sum over all n numbers is negative. ... Unfortunately that algorithm does not scale. ... An instance of 3-Partition consists of a list of 3m positive integers ... list can be partitioned into m lists of size 3, ...
    (sci.math)
  • Re: Summing numbers from a list to a goal
    ... unique) that sum up to the goal sum plus or minus the acceptable ... Sort the lists of subset sums. ...
    (comp.programming.contests)
  • Re: Simple recursive functions in Lisp
    ... programmers working with code. ... (defun sum (list) ... (loop for x in list sum x)) ... Why is loop disqualified as "higher level"? ...
    (comp.lang.lisp)