masukomi
masukomi

Reputation: 10902

How do i convert a string to a quoted variable

Lets say i want to get the documentation for a function, I'd say

(documentation 'foo 'function)

but what if I only had foo and function as strings? E.g. "foo" and "function".

What would I have to do to them to make them usable as parameters to the documentation call?

[Side note: I'm using clisp, but I doubt that matters.]

Upvotes: 2

Views: 216

Answers (2)

uselpa
uselpa

Reputation: 18927

Use INTERN to convert a string to a symbol. Make sure you uppercase the strings because, unlike with symbols, the reader will not do it for you:

(tested in SBCL):

* (documentation 'mapcar 'function)
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
   return values."

* (documentation (intern "MAPCAR") (intern "FUNCTION"))
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
   return values."

Upvotes: 1

Rainer Joswig
Rainer Joswig

Reputation: 139401

Use FIND-SYMBOL, not INTERN. If you want to find documentation for an existing function, finding a symbol is enough. INTERN also creates symbols.

CL-USER > (find-symbol "SIN" "COMMON-LISP")
SIN
:EXTERNAL

Note that Common Lisp symbols are uppercase internally be default. Thus you need to use an uppercase string to find the corresponding symbol in the corresponding package.

Also note that there actually isn't something like a 'quoted variable'. You want to convert a string to a symbol.

Upvotes: 8

Related Questions