Page 1 of 2

convert char to symbol

Posted: Thu Jan 31, 2013 8:40 am
by santiago
Hi,

Related to this other post http://lispforum.com/viewtopic.php?f=2&t=4091

If have this function:

(defun char1()
(let ((mychar (read-char *standard-input*)))
'y
)
)

always return: Y

This other function:

(defun char2()
(let ((mychar (read-char *standard-input*)))
mychar
)
)

always return: #\y


(defun char3()
(let ((mychar (read-char *standard-input*)))
(string mychar)
)
)

always return: "y"

The only function that works fine is the first, because its returning only the literal y, but I don't want to hardcode the y, I need to obtain from the user.

I need that the char readed can be converted to a symbol but I don't know how. The second function returns a char with #\. The third function return a string with doubles quotes, but I need to return a symbol, not char #\ or not string "". Is there any function to convert char to symbol?

Thanks

Re: convert char to symbol

Posted: Thu Jan 31, 2013 10:48 am
by pjstirling

Code: Select all

(DEFUN char-to-symbol (char)
  (INTERN (STRING-UPCASE (MAKE-STRING 1 :INITIAL-ELEMENT char)))
INTERN can take a package as the second argument if you want the symbol to be created in a different package. STRING-UPCASE is required if you want to interact with symbols created as a normal part of code compilation.

Re: convert char to symbol

Posted: Thu Jan 31, 2013 1:32 pm
by sylwester
Are you really sure you want to convert a char to a symbol? Yes, the representation of a char y when returned in a REPL is #\y and it's nice since it's what you need to feed it for it to understand it's the character y. It's the same for java, but it's 'y'. If java had a REPL it surely would print 'y' instead of just y.

(princ #\y) ; prints just out one single y, but returns it's argument IF run in a repl. If you make a program that (princ #\y) you will only see the outputted y.

Re: convert char to symbol

Posted: Fri Feb 01, 2013 4:47 am
by santiago
Hi,

When I try to execute this function: char-to-symbol, appears this error

APPLY: too few arguments given to CHAR

What this mean?

Thanks

Re: convert char to symbol

Posted: Fri Feb 01, 2013 6:29 am
by marcoxa
santiago wrote:Hi,

When I try to execute this function: char-to-symbol, appears this error

APPLY: too few arguments given to CHAR

What this mean?

Thanks
How do you call the function at the prompt? I have the suspicion that you are still confusing a few things....

MA

Re: convert char to symbol

Posted: Fri Feb 01, 2013 6:46 am
by santiago
I Call the function like this: char-to-symbol(char)

Thanks

Re: convert char to symbol

Posted: Fri Feb 01, 2013 8:42 am
by Goheeca
An invocation of a function looks:

Code: Select all

(char-to-symbol char)
where char is a variable.

Re: convert char to symbol

Posted: Fri Feb 01, 2013 10:30 am
by marcoxa
santiago wrote:I Call the function like this: char-to-symbol(char)

Thanks
Looks wrong to me.

You should be doing something like:

Code: Select all

cl-prompt> (char-to-symbol #\C)
C

Re: convert char to symbol

Posted: Sun Feb 03, 2013 10:55 am
by santiago
Hi,

But I need that #\C is a variable, not a literal

Thanks

Re: convert char to symbol

Posted: Sun Feb 03, 2013 11:15 am
by Goheeca
So you want to create a variable with a name which is known as an argument, why would you do that? are you writing some kind of transpiler? I ask because I need to know what you really want to do with that to give you a good advice.