convert char to symbol
convert char to symbol
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
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
-
- Posts: 166
- Joined: Sun Nov 28, 2010 4:21 pm
Re: convert char to symbol
Code: Select all
(DEFUN char-to-symbol (char)
(INTERN (STRING-UPCASE (MAKE-STRING 1 :INITIAL-ELEMENT char)))
Re: convert char to symbol
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.
(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
Currently I'm planning a Scheme compiler :p
Re: convert char to symbol
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
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
How do you call the function at the prompt? I have the suspicion that you are still confusing a few things....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
MA
Marco Antoniotti
Re: convert char to symbol
I Call the function like this: char-to-symbol(char)
Thanks
Thanks
Re: convert char to symbol
An invocation of a function looks:
where char is a variable.
Code: Select all
(char-to-symbol char)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
Re: convert char to symbol
Looks wrong to me.santiago wrote:I Call the function like this: char-to-symbol(char)
Thanks
You should be doing something like:
Code: Select all
cl-prompt> (char-to-symbol #\C)
C
Marco Antoniotti
Re: convert char to symbol
Hi,
But I need that #\C is a variable, not a literal
Thanks
But I need that #\C is a variable, not a literal
Thanks
Re: convert char to symbol
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). Temporary mirrors of aferomentioned: CLHS and a dark version.