Page 2 of 2

Re: convert char to symbol

Posted: Mon Feb 04, 2013 8:10 am
by santiago
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

Posted: Mon Feb 04, 2013 8:36 am
by Goheeca
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):

Code: Select all

(setf (readtable-case *readtable*) :preserve)
And I ask again why you need a symbol and not a character?

Re: convert char to symbol

Posted: Mon Feb 04, 2013 9:02 am
by santiago
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

Posted: Mon Feb 04, 2013 10:00 am
by Goheeca
Instead of printing you can get returned a string:

Code: Select all

(format nil "Hello ~a" "world!")
And yes, you can capture the standard output:

Code: Select all

(with-output-to-string (*standard-output*) (format t "Hello ~a" "world!"))
or use another stream:

Code: Select all

(with-output-to-string (stream) (format stream "Hello ~a" "world!"))
or like that:

Code: Select all

(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:

Code: Select all

(princ "Hello world!")
or:

Code: Select all

(format t "~a" "Hello world")

Re: convert char to symbol

Posted: Mon Feb 04, 2013 11:03 am
by santiago
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

Posted: Mon Feb 04, 2013 11:16 am
by Goheeca

Code: Select all

(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!

Re: convert char to symbol

Posted: Mon Feb 04, 2013 11:40 am
by sylwester
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:

Code: Select all

(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.

Re: convert char to symbol

Posted: Wed Feb 06, 2013 8:42 am
by santiago
sylwester works fine !!

This is what I need

Thanks