This will print data, but I want it to print show. I want to print the value, not the expression, how would I do it?
(defun display (x)
(list x))
(setq temp 'data)
(set temp 'show)
(display 'data)
what if u dont know if the variable is bound or not? i have to write a function that take a key and a value, if key does not exist, then i have to do setq key value, if the key already exist, then i would add the value to the key. In this case, if i do (storedata key value), if value have not been bounded, i get an unbound error, how would i handle this case?
for example, if there is no mydata and i do (storedata value mydata) then mydata would become (value), now if i do (storedata value2 mydata) then mydata becomes (value value2).
Quoting a list or a symbol in Lisp with
'is exactly equivalent to using the special form(quote ...). It’s specifically for making the quoted thing not get evaluated.'datain Lisp code or typed into the REPL is the same thing as(quote data), and evaluates to the symboldata.datawithout the quote evaluates to the value of the variabledatain the current scope. So, at the REPL:The first expression also evaluates to
14becausesetqreturns the value of the bound variable (in this respect acting like the assignment operator=in C).What you’ve done in the above code is to set the variable named
tempto contain the symboldata, then, by usingset(without thesetq), set the variable nameddatato the symbolshow. This is a bit similar to using soft references in Perl (for example), but I don’t think it’s particularly widely used or advisable as a Lisp technique.By the way, your
displayprocedure is probably not doing what you think either: it returns a single element list of whatever you pass to it. The fact that the value gets printed when you type it into the REPL is just because the value of any expression gets printed at the REPL. To display a value in a program you might useprintor maybe format. (I’m assuming you’re using Common Lisp, since it’s obviously not Scheme, but maybe it’s some other Lisp variety, in which case that link won’t help.)