a very simple function

Discussion of Common Lisp
Post Reply
Posts: 1
Joined: Sun Mar 13, 2011 3:09 pm

a very simple function

Post by mollens » Sun Mar 13, 2011 3:18 pm

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

Posts: 20
Joined: Fri Jan 28, 2011 6:13 pm

Re: a very simple function

Post by jstoddard » Sun Mar 13, 2011 8:17 pm

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

Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta

Re: a very simple function

Post by Warren Wilkinson » Tue Mar 15, 2011 7:35 pm

Another way is to use rplacd [ or (setf (cdr ...) ...)] on the last element of the list to destructively set it to (list N).
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

Posts: 6
Joined: Tue Feb 08, 2011 10:22 am

Re: a very simple function

Post by churib » Sun Mar 20, 2011 7:30 am

Another (non-destructive) solution:

Code: Select all

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

Post Reply