Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Common Lisp, is there a function that returns a symbol from a given string?

I want

>(??? "car")
CAR
>((??? "car") '(1 2))
1 

I can't seem to find a function that does this.

like image 834
user342580 Avatar asked Feb 18 '11 06:02

user342580


3 Answers

There are a few, depending on exactly what you're wanting to do.

First, intern, this will return an existing symbol by that name, if it exists and will otherwise create a new one.

Second, find-symbol, this will return the symbol, if it exists and nil otherwise (it has two return values, the second can be used to distinguish between "returning nil as the symbol" and "returning nil as no symbol found").

Third, there is make-symbol, this will always create a new, uninterned symbol and is almost guaranteed to not be what you want in this specific case.

like image 57
Vatine Avatar answered Sep 28 '22 07:09

Vatine


Are you looking for this?

(eval (read-from-string "(car '(1 2))"))

Gives: 1


UPDATE:

How about (funcall (intern "CAR") '(1 2)) ? :)

like image 32
Johan Kotlinski Avatar answered Sep 28 '22 05:09

Johan Kotlinski


>(??? "car")
CAR
>((??? "car") '(1 2))
1 

use:

CL-USER 17 > (find-symbol "CAR")
CAR
:INHERITED

CL-USER 18 > (funcall (find-symbol "CAR") '(1 2))
1

Note that the names of symbols are internally UPPERCASE in Common Lisp. FUNCALL allows us to call a symbol as a function. One can also use a function object with FUNCALL.

You can also create a form and EVAL that:

CL-USER 19 > (eval `(,(find-symbol "CAR") '(1 2)))
1

or

CL-USER 20 > (eval (list (find-symbol "CAR") ''(1 2)))
1
like image 44
Rainer Joswig Avatar answered Sep 28 '22 06:09

Rainer Joswig