Is newLISP "prejudiced" against recursive function

Started by rickyboy, April 14, 2005, 08:55:14 PM

Previous topic - Next topic

rickyboy

Hello everyone,



Consider this function which adjoins one element 'x' to a set 'S', non-destructively.
(define (adjoin1 S x)
  (if (or (= x nil) (= S nil) (member x S))
      S
      (cons x S)))
> (define L '(1 2 3))
(1 2 3)
> (adjoin1 L 0)
(0 1 2 3)


Now to adjoin more than one element (at a time), it seems natural to then say
(define (adjoin S)
  (if (empty? (args))
      S
      (apply adjoin
             (cons (adjoin1 S (first (args)))
                   (rest (args))))))

but this does not work --- it fails on a 'call stack overflow'.



Now, yes, I know that the alternative definition
(define (adjoin S)
  (if (empty? (args))
      S
      (let ((result S))
        (dolist (x (args))
          (set! result (adjoin1 result x))))))

works and indeed, there are other ways also, for instance:
(define (adjoin S) (unique (append S (args))))

However, why doesn't the first (recursive) definition of 'adjoin' work?  (I like the last definition, but I may need to define a recursive function in the future and so still need to figure out what is going on with the first definition.)



Thanks!  --Ricky
(λx. x x) (λx. x x)

Lutz

#1
args was not set on deeper recursion levels when empty. This is a bug which is fixed in todays development release 8.5.3 here: http://newlisp.org/downloads/development/">http://newlisp.org/downloads/development/



Lutz



ps: your first solution works now, but existing multiples of list elements will not be uniqued as in your last version, which if of course the best.

rickyboy

#2
Thanks Lutz!
(λx. x x) (λx. x x)