Page 1 of 1

append with first argument optional nil

Posted: Mon Mar 21, 2005 12:34 pm
by HPW
Attempt to made a more alisp compatibel append:

Code: Select all

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

Code: Select all

> (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?

Posted: Mon Mar 21, 2005 1:31 pm
by Lutz
Use 'apply append' instead of 'dolist':

Code: Select all

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

Posted: Mon Mar 21, 2005 2:33 pm
by HPW
Thanks a lot. I have still the problem, to get the brain more lispisch.
;-)

Posted: Mon Mar 21, 2005 3:51 pm
by Lutz
we can make it even shorter:

Code: Select all

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