How can I convert char to symbol in common lisp?

Viewed 1538

Total lisp beginner here.

I'm wondering how to convert a character to symbol. Simply what I want is convert #\a to a

Here what I have done so far:

(defun convert(char)
(if(eq char #\a)
    (setq char 'a))
    char)

This one is works actually but I don't want to add 26 conditions(letters in alphabet) and make a long-dumb code.

Also I'm wondering is there any functions in common lisp that converts character list to symbol list like: (#\h #\e #\l #\l #\o) to (h e l l o) ? I have found intern and make-symbol related to that but they require string as parameter.

3 Answers
CL-USER 230 > (intern (string #\Q))
Q
NIL

CL-USER 231 > (intern (string #\q))
\q
NIL

Btw., your code has a bunch of improvements necessary:

(defun convert(char)   ; 
(if(eq char #\a)       ; use EQL instead of EQ for characters
                       ; indentation is wrong
    (setq char 'a))    ; indentation is wrong
    char)              ; indentation is wrong

Better write it as:

(defun convert (char)
  (if (eql char #\a)
      'a
      char))

or

(defun convert (char)
  (case char
    (#\a 'a)
    (#\b 'b)
    (otherwise char)))

As mentioned above, the 'real' solution is:

(defun convert (char)
  (intern (string char)))
(defun converter (c)
  (if (characterp c)
      (make-symbol (string c))))

You could use make-symbol and convert the symbol to a string.

(setq my-sym (converter #\a))

Answering my own question after a while:

Also I'm wondering is there any functions in common lisp that converts character list to symbol list like: (#\h #\e #\l #\l #\o) to (h e l l o) ?

I can convert single character to symbol with this convert function:

(defun convert (c)
    (if (characterp c)
        (intern (string c))))

Since one of main ideas of LISP is "processing a list" I can use one of map functions to apply one operation to every single element of the list.

(mapcar #'convert '(#\h #\e #\l #\l #\o))

Here mapcar function will result the list of symbols:

(|h| |e| |l| |l| |o|)
Related