append with first argument optional nil

Started by HPW, March 21, 2005, 04:34:11 AM

Previous topic - Next topic

HPW

Attempt to made a more alisp compatibel append:



(define (append-nil alist )
(if alist
(begin
(setq _nlst alist)
(dolist (_lst (args))(setq _nlst(append _nlst _lst))))
(begin
(setq _nlst (list ))
(dolist (_lst (args))(setq _nlst(append _nlst _lst))))
))



> (setq a '(1 2))
(1 2)
> (append-nil a '(3)'(4)'(5))
(1 2 3 4 5)
> (append-nil b '(3)'(4)'(5))
(3 4 5)
>


Any better solution?
Hans-Peter

Lutz

#1
Use 'apply append' instead of 'dolist':

(define (append-nil alist)
  (if alist (append alist (apply append (args)))
            (apply append (args))))


Lutz

HPW

#2
Thanks a lot. I have still the problem, to get the brain more lispisch.

;-)
Hans-Peter

Lutz

#3
we can make it even shorter:

(define (append-nil alist)
  (append (if alist alist '()) (apply append (args))))


Lutz