Reputation: 1009
For example:
(setf s 2)
s => 2
(setf list1 '(1 s 3 4))
list1 => (1 s 3 4)
How do i get it to add the value stored in s to the list? For this example I would want to use s to generate a list (1 2 3 4) I have a lisp book I'm reading and I can't seem to find any mention of how to do this so I thought i'd ask. Thanks
Upvotes: 1
Views: 70
Reputation: 139411
I would want to use s to generate a list (1 2 3 4)
The function list
can be handy:
CL-USER 14 > (let ((s '2))
(list 1 s 3 4))
(1 2 3 4)
The function LIST
creates a fresh new list from its arguments.
Upvotes: 1
Reputation: 48775
So quoted data in Scheme are like String constants.. If I wrote "1 s 3 4"
in Java I wouldn't be able to get s
replaced with the variable contents. I 'd have to write "1 " + s + " 3 4"
. In Lisp we have backquote to do this in list structures:
`(1 ,s 3 4)
; ==> (1 2 3 4)
Note that this is a trick.. It's like "1 $s 3 4" in PHP as it's representing code that creates the list with the unquoted variables evaluated and return a new list structure. Under the hood it's very similar to writing:
(list 1 s 3 4)
; ==> (1 2 3 4)
And of course list
is not a primitive since it just uses cons
. What it does is this:
(cons 1 (cons s (cons 3 (cons 4 '()))))
; ==> (1 2 3 4)
Upvotes: 1