Page 1 of 1

a very simple function

Posted: Sun Mar 13, 2011 3:18 pm
by mollens
Hi,
I'm really new at the lisp and I need your help.
I have got a list

Code: Select all

(setq S '(5 6 4 2))
When I call some function which looks like

Code: Select all

(defun add (S N)  (...) )
where S is the list and the N is a number.
This number(N) I want to add at the end of the list. The result should be
(5 6 4 2 N).
For example N=7 then the list is (5 6 4 2 7).
How can I do it ? Please help me.

Thanks in advance. Mike

Re: a very simple function

Posted: Sun Mar 13, 2011 8:17 pm
by jstoddard
One possible way to go about it would be to use reverse and push. Here's the descriptions of those:

http://www.lispworks.com/documentation/ ... revers.htm
http://www.lispworks.com/documentation/ ... m_push.htm

It shouldn't be too hard to work out the details...

Re: a very simple function

Posted: Tue Mar 15, 2011 7:35 pm
by Warren Wilkinson
Another way is to use rplacd [ or (setf (cdr ...) ...)] on the last element of the list to destructively set it to (list N).

Re: a very simple function

Posted: Sun Mar 20, 2011 7:30 am
by churib
Another (non-destructive) solution:

Code: Select all

(append S (list N))
See http://www.lispworks.com/documentation/ ... append.htm