tags:

views:

497

answers:

3

Is there a function in Common Lisp that takes a string as an argument and returns a keyword?

Example: (keyword "foo") -> :foo

A: 
(intern "foo") -> :foo

See the Strings section of the Common Lisp Cookbook for other string/symbol conversions and a detailed discussion of symbols and packages.

Jonathan Wright
The name needs to be interned in the "KEYWORD" package to be a keyword. e.g., (intern "FOO" "KEYWORD")
Chris Jester-Young
ah yes. The (intern "foo" "KEYWORD") works quite nicely. Thank you.
nathan
In my answer, I've packaged it up into a neat little function, which you may enjoy. :-)
Chris Jester-Young
+7  A: 

Here's a make-keyword function which packages up keyword creation process (interning of a name into the KEYWORD package). :-)

(defun make-keyword (name) (values (intern name "KEYWORD")))
Chris Jester-Young
+3  A: 

The answers given while being roughly correct do not produce a correct solution to the question's example.

Consider:

CL-USER(4): (intern "foo" :keyword)

:|foo|
NIL
CL-USER(5): (eq * :foo)

NIL

Usually you want to apply STRING-UPCASE to the string before interning it, thus:

(defun make-keyword (name) (values (intern (string-upcase name) "KEYWORD")))
skypher