nondestructive push

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

nondestructive push

Post 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.
WBR, Dmi

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Thats destructive, DMI ment non-destrutive ;-)
-- (define? (Cornflakes))

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Its surly is ...:)
-- (define? (Cornflakes))

Locked