append with first argument optional nil

Q&A's, tips, howto's
Locked
HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

append with first argument optional nil

Post 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?
Hans-Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Thanks a lot. I have still the problem, to get the brain more lispisch.
;-)
Hans-Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

we can make it even shorter:

Code: Select all

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

Locked