convert char to symbol

Discussion of Common Lisp

convert char to symbol

Postby santiago » Thu Jan 31, 2013 8:40 am

Hi,

Related to this other post 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
santiago
 
Posts: 17
Joined: Wed Feb 22, 2012 8:44 am

Re: convert char to symbol

Postby pjstirling » Thu Jan 31, 2013 10:48 am

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.
pjstirling
 
Posts: 83
Joined: Sun Nov 28, 2010 4:21 pm

Re: convert char to symbol

Postby sylwester » Thu Jan 31, 2013 1:32 pm

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.
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 84
Joined: Mon Jul 11, 2011 2:53 pm

Re: convert char to symbol

Postby santiago » Fri Feb 01, 2013 4:47 am

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
santiago
 
Posts: 17
Joined: Wed Feb 22, 2012 8:44 am

Re: convert char to symbol

Postby marcoxa » Fri Feb 01, 2013 6:29 am

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
Marco Antoniotti
marcoxa
 
Posts: 70
Joined: Thu Aug 14, 2008 6:31 pm

Re: convert char to symbol

Postby santiago » Fri Feb 01, 2013 6:46 am

I Call the function like this: char-to-symbol(char)

Thanks
santiago
 
Posts: 17
Joined: Wed Feb 22, 2012 8:44 am

Re: convert char to symbol

Postby Goheeca » Fri Feb 01, 2013 8:42 am

An invocation of a function looks:
Code: Select all
(char-to-symbol char)

where char is a variable.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 220
Joined: Thu May 10, 2012 12:54 pm

Re: convert char to symbol

Postby marcoxa » Fri Feb 01, 2013 10:30 am

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
Marco Antoniotti
marcoxa
 
Posts: 70
Joined: Thu Aug 14, 2008 6:31 pm

Re: convert char to symbol

Postby santiago » Sun Feb 03, 2013 10:55 am

Hi,

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

Thanks
santiago
 
Posts: 17
Joined: Wed Feb 22, 2012 8:44 am

Re: convert char to symbol

Postby Goheeca » Sun Feb 03, 2013 11:15 am

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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 220
Joined: Thu May 10, 2012 12:54 pm

Next

Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests