• Re: Simple recursive functions in Lisp

    From B. Pym@21:1/5 to B. Pym on Sun Jul 14 19:56:39 2024
    B. Pym wrote:

    Pascal Bourguignon wrote:

    (defun sum (list)
    (loop :for x :in list :sum x))

    Gauche Scheme

    (define (sum lst) (fold + 0 lst))

    (sum '(1 3 5 7 9))
    ===>
    25

    Pascal Bourguignon wrote:

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

    Better:

    (define (sum lst)
    (do ((xs lst (cdr xs))
    (res 0 (+ res (car xs))))
    ((null? xs) res)))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Pascal Bourguignon on Sun Jul 14 19:46:50 2024
    Pascal Bourguignon wrote:

    (defun sum (list)
    (loop :for x :in list :sum x))

    Gauche Scheme

    (define (sum lst) (fold + 0 lst))

    (sum '(1 3 5 7 9))
    ===>
    25

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)