Page 1 of 1

nondestructive push

Posted: Wed Nov 09, 2005 10:49 pm
by Dmi
How can I construct a function, that nondestructive appends an element to a list and return modified list?
I wrote so:

Code: Select all

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

Posted: Thu Nov 10, 2005 8:38 am
by pjot

Code: Select all

(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

Posted: Thu Nov 10, 2005 2:54 pm
by newdep
Thats destructive, DMI ment non-destrutive ;-)

Posted: Thu Nov 10, 2005 3:05 pm
by pjot
It's non destructive:

Code: Select all

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

Posted: Thu Nov 10, 2005 8:39 pm
by newdep
Its surly is ...:)