convert char to symbol
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
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):
And I ask again why you need a symbol and not a character?
Code: Select all
(setf (readtable-case *readtable*) :preserve)
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
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:
And yes, you can capture the standard output:
or use another stream:
or like that:
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:
or:
Code: Select all
(format nil "Hello ~a" "world!")
Code: Select all
(with-output-to-string (*standard-output*) (format t "Hello ~a" "world!"))
Code: Select all
(with-output-to-string (stream) (format stream "Hello ~a" "world!"))
Code: Select all
(let ((stream (make-string-output-stream)))
(format stream "Hello ~a" "world!")
(get-output-stream-string s))
Code: Select all
(princ "Hello world!")
Code: Select all
(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
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
Code: Select all
(defun return-a-char ()
(intern (coerce (list (read-char)) 'string)))
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
Your variable that evaluates to a literal is just placed in place of the literal, like this:santiago wrote:Hi,
But I need that #\C is a variable, not a literal
Thanks
Code: Select all
(setq var #\C)
(char-to-symbol var)
==> C
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
sylwester works fine !!
This is what I need
Thanks
This is what I need
Thanks