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
(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
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
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
Re: convert char to symbol
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
Re: convert char to symbol
I Call the function like this: char-to-symbol(char)
Thanks
Re: convert char to symbol
An invocation of a function looks:
(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). Temporary mirrors of aferomentioned:
CLHS and a
dark version.
Re: convert char to symbol
santiago wrote:I Call the function like this: char-to-symbol(char)
Thanks
Looks wrong to me.
You should be doing something like:
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
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.
Re: convert char to symbol
Hi,
I have this function that works fine with Java:
(defun char1()
(let ((mychar (read-char *standard-input*)))
'y
)
)
But I need transform this function to return 'y (as a symbol), but using a variable (mychar). If I try to return mychar, I obtain #\y, and If I try to return a string, I obtain "y".
I only want to transform this function to return only y without quotes or without #\ but the only way is to use 'y, but I need to use a variable.
Thanks
Re: convert char to symbol
But it's not a variable, it's only a symbol what is returned. The solution is provided by
pjstirling. And if you want to see a lowercase symbol without escape characters you can set up
it (after that you must use uppercase for the standard CL API):
(setf (readtable-case *readtable*) :preserve)
And I ask again why you need a symbol and not a character?
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
Hi.
Something like that I need is using the format function:
(defun working-chars ()
(let ((mychar (read-char *standard-input*)))
(format t "~:c" mychar)
)
)
But this print in the standard-output, the y character that I need, but I don't want to print to the output, I want to return the mychar, no print it.
¿It's possible to asign the format output to a variable in lisp, like this (setq express (format t "~:c" mychar)) ?
Thanks
Re: convert char to symbol
Instead of printing you can get returned a string:
(format nil "Hello ~a" "world!")
And yes, you can capture the standard output:
(with-output-to-string (*standard-output*) (format t "Hello ~a" "world!"))
or use another stream:
(with-output-to-string (stream) (format stream "Hello ~a" "world!"))
or like that:
(let ((stream (make-string-output-stream)))
(format stream "Hello ~a" "world!")
(get-output-stream-string s))
Of course the returned value will be wrapped by double quotes, it's because of an ability read the value back. You can print a string like this:
(princ "Hello world!")
or:
(format t "~a" "Hello world")
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
Hi,
I need to return the literal or the symbol of a character... I can't return a char, because I return #\ and I can't return a string, because I return "".
In this function:
(defun return-a-char()
(let ((mychar (read-char *standard-input*)))
(format t "~:c" mychar)
)
)
If I enter y, then I print y (without quotes, or without #\). The problem is that I need to return something, not to print to format. This is why If I rewrite the function like this:
(defun return-a-char()
(let ((mychar (read-char *standard-input*)))
'y
)
)
It's works fine, because I return the literal y, without quotes of the string and without #\ of chars, but I don't return the value of the variable.... only a literal. What I need is to return the char value of the variable without quotes and without #\
Thanks
Re: convert char to symbol
(defun return-a-char ()
(intern (coerce (list (read-char)) 'string)))
Probably it's not a good approach to the problem. Read something about
intern and
symbol and realize that symbols are a special data type it's like identifiers in Java and there you don't return them too!
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
santiago wrote:Hi,
But I need that #\C is a variable, not a literal
Thanks
Your variable that evaluates to a literal is just placed in place of the literal, like this:
(setq var #\C)
(char-to-symbol var)
==> C
I'm still baffled by the fact you need this. #\C, 'C and "C" look the same when printed (to a socket, pipe, etc). The only way your java process see #\C and "C" is if your program interfaces with the REPL.
I'm the author of
two useless languages that uses
BF as target machine.
Currently I'm planning a Scheme compiler :p
Re: convert char to symbol
sylwester works fine !!
This is what I need
Thanks