Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Lisp Programmatic Keyword

Tags:

common-lisp

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

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

like image 718
nathan Avatar asked Oct 17 '08 10:10

nathan


3 Answers

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"))) 
like image 198
Leslie P. Polzer Avatar answered Sep 24 '22 04:09

Leslie P. Polzer


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")))
like image 32
Chris Jester-Young Avatar answered Sep 25 '22 04:09

Chris Jester-Young


There is a make-keyword function in the Alexandria library, although it does preserve case so to get exactly what you want you'll have to upcase the string first.

like image 21
Samuel Edwin Ward Avatar answered Sep 23 '22 04:09

Samuel Edwin Ward