newLISP Fan Club

Forum => Anything else we might add? => Topic started by: Dmi on November 09, 2005, 02:49:02 PM

Title: nondestructive push
Post by: Dmi on November 09, 2005, 02:49:02 PM
How can I construct a function, that nondestructive appends an element to a list and return modified list?

I wrote so:
(define (print-ln) (print (join (append (args) (list "n")))))
But the idea is so usual, that I suspect, there is more fine way, without using

(append lst (list one-element))



... I use 'print-ln' insead of println under windows to handle unix-generated text files.
Title:
Post by: pjot on November 10, 2005, 12:38:26 AM

(define (mypush) (flat (append (args))))

> (set 'lst (mypush 1 2 3))
(1 2 3)
> (mypush lst 4)
(1 2 3 4)


...or is this not what you want?



Peter
Title:
Post by: newdep on November 10, 2005, 06:54:49 AM
Thats destructive, DMI ment non-destrutive ;-)
Title:
Post by: pjot on November 10, 2005, 07:05:16 AM
It's non destructive:



> (set 'lst (mypush 1 2 3))
(1 2 3)
> (mypush lst 4)
(1 2 3 4)
> (println lst)
(1 2 3)
Title:
Post by: newdep on November 10, 2005, 12:39:16 PM
Its surly is ...:)