lists

Started by newdep, February 25, 2004, 12:59:36 PM

Previous topic - Next topic

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.
-- (define? (Cornflakes))

Lutz

#1
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

newdep

#2
;-)



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



Thanks Lutz...
-- (define? (Cornflakes))