lists

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

lists

Post by newdep »

Hello All,

Oooo im probably making the "newbe" greeting posting this one ;-)

Im trying to extend a list, now im using 'append or 'cons still the original
variable stays untouched (featured) when i dont use an extra (set 'var ...)

below the example..

> (set 'alist (list '(something)))
((something))

> (set 'alist (cons alist '(hello) ))
(((something)) hello)

> alist
(((something)) hello)

> (set 'alist ( append alist (list '(someone)) ) )
(((something)) hello (someone))

> alist
(((something)) hello (someone))


--- but is it possible to extend a list without the use of the "extra" 'set?
--- so that the original 'list is updated?

like ->

(append alist (list '(to-add)))

or

(cons alist '(new-one))



Regards,
Norman.

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

Post by Lutz »

Quick answer:

(set 'mylist '(b c))
(push 'a mylist)

alist => (a b c)

Long answer:

By definition most functions in a 'functional' language to not change the contents of their arguments. If they do change their arguments they are defined as having a side effect or they are said to be destructive functions. In newLISP like in all functional languages only few functions have such side effect they are listed in the manual in the chapter "Destructive versus non-destructive functions". In the example above 'push' is a destructive function changing the contents of the second of its arguments.

A good LISP programmer tries to stay pure functional most of the time using destructive functions only sparingly.

McCarthy (the orignal inventor of LISP) proved that any algorithm/function can be formulated completely without side effect with only a handful functions.

Lutz
Last edited by Lutz on Wed Feb 25, 2004 9:24 pm, edited 2 times in total.

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

Post by newdep »

;-)

I was pushing the wrong way.. (push 'value 'list)..

Thanks Lutz...

Locked