Is newLISP "prejudiced" against recursive function
Posted: Fri Apr 15, 2005 3:55 am
Hello everyone,
Consider this function which adjoins one element 'x' to a set 'S', non-destructively.
Now to adjoin more than one element (at a time), it seems natural to then say
but this does not work --- it fails on a 'call stack overflow'.
Now, yes, I know that the alternative definition
works and indeed, there are other ways also, for instance:
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
Consider this function which adjoins one element 'x' to a set 'S', non-destructively.
Code: Select all
(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)
Code: Select all
(define (adjoin S)
(if (empty? (args))
S
(apply adjoin
(cons (adjoin1 S (first (args)))
(rest (args))))))
Now, yes, I know that the alternative definition
Code: Select all
(define (adjoin S)
(if (empty? (args))
S
(let ((result S))
(dolist (x (args))
(set! result (adjoin1 result x))))))
Code: Select all
(define (adjoin S) (unique (append S (args))))
Thanks! --Ricky