convert char to symbol

Discussion of Common Lisp
santiago
Posts: 17
Joined: Wed Feb 22, 2012 8:44 am

Re: convert char to symbol

Post by santiago » Mon Feb 04, 2013 8:10 am

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: convert char to symbol

Post by Goheeca » Mon Feb 04, 2013 8:36 am

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

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

Re: convert char to symbol

Post by santiago » Mon Feb 04, 2013 9:02 am

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: convert char to symbol

Post by Goheeca » Mon Feb 04, 2013 10:00 am

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")
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.

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

Re: convert char to symbol

Post by santiago » Mon Feb 04, 2013 11:03 am

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: convert char to symbol

Post by Goheeca » Mon Feb 04, 2013 11:16 am

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

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

Re: convert char to symbol

Post by sylwester » Mon Feb 04, 2013 11:40 am

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.
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

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

Re: convert char to symbol

Post by santiago » Wed Feb 06, 2013 8:42 am

sylwester works fine !!

This is what I need

Thanks

Post Reply