#\ vs '

Discussion of Common Lisp

#\ vs '

Postby santiago » Wed Jan 30, 2013 12:11 pm

Hi,

what is the difference between this expression \#y and this other 'y?

I'm trying to read a char from Java and I've problems using \# or aphostrophe '

Any example?

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

Re: #\ vs '

Postby Goheeca » Wed Jan 30, 2013 12:32 pm

In CL an apostrophe means a quotation so 'y is a quoted one-char symbol. #\y is the standard CL representation of character.
Furthermore, I misapprehend your problem, can you elaborate on the reading of char from Java? And what is \#?
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 198
Joined: Thu May 10, 2012 12:54 pm

Re: #\ vs '

Postby edgar-rft » Wed Jan 30, 2013 5:47 pm

\# is the symbol |#|
\#y is the symbol |#Y|
#\y is a read-macro for the character y, see CLHS 2.4.8.1 Sharpsign Backslash
'y is a read-macro for (quote y), see CLHS 2.4.3 Single-Quote and Special Operator QUOTE

- edgar
edgar-rft
 
Posts: 154
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: #\ vs '

Postby santiago » Thu Jan 31, 2013 12:57 am

Hi,

I'm reading a char from my Java application and passing to the LISP using sockets. Then in LISP I read the char using read-char and assing it to the variable char. Then I try to evaluate the variable char with the characters y or n using #\y, #\n and if is true, I return 'y or 'n

(let ((char (read-char *standard-input*)))
(cond ((eql char #\y) 'y)
((eql char #\Y) 'y)
((eql char #\n) 'n)
((eql char #\N) 'N)
(t (char))
)
)

My problem is that I don't want to return 'y or 'n, because I want to return the value of the char variable.

If I try this:

((eql char #\y) char) it doesn't work but if I try this ((eql char #\y) 'y) it works fine. That's why I don't understand what's the difference and why I can't return the char variable. I undestand that the char variable has the #\y value.

It's possible that the char variable has another characters like return?

Is there any function in LISP to pass characters from #\y to 'y?

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

Re: #\ vs '

Postby JamesF » Thu Jan 31, 2013 3:00 am

If you want to return the character itself, (format nil "~A" char) would be the most convenient way to convert it into a string, which I'm guessing is the effect you want. If char has the value #\y, it will return
the string "y".
The problem with returning 'y is that this is a symbol. In Lisp, this is a very different thing from a character or a string - CL serialises them in different ways. You can convert 'y to "y", but it's better not to create that problem in the first place.

In case that doesn't answer the question, when you say that it doesn't work, what error do you get? That would help us figure out what's breaking and where.

One last thing: (t (char)) doesn't do what you probably think it does. It means that the default case is to evaluate the function #'char with no arguments, and to return the result. (t char), or possibly (t (format nil "~A" char)) is much more likely to work the way you want.
JamesF
 
Posts: 95
Joined: Thu Jul 10, 2008 7:14 pm

Re: #\ vs '

Postby santiago » Thu Jan 31, 2013 3:36 am

James,

There's no error in LISP, only in the application. Because the application can't read y, the application stops....

This was the original function that returns a char and works fine....

(def$method (tty-dialog-mixin :babylon-read) (&optional special-keys)
"reads in a character or a lisp form from dialog-stream.
only those characters occurring in the list special-keys are read."

(let ((char (read-char dialog-stream)))
(cond ((member char special-keys :test 'char-equal)
(clear-input dialog-stream)
char)
(t (prog2 (unread-char char dialog-stream)
(read dialog-stream)
(clear-input dialog-stream))))))


and this was the function that I changed because I need put this character "¤" to stop Java process and wait the lisp process

(def$method (tty-dialog-mixin :babylon-read) (&optional special-keys)
(format t "¤")
(let ((char (read-char *standard-input*)))
(format t " --- what appears here ----")
(format t (string char))
(cond ((eql char #\y) char)
((eql char #\Y) char)
((eql char #\n) char)
((eql char #\N) char)
(t (char))
)
)
)

I don't understand, why I can't return char, but if I return the symbol works fine: if I return 'y works fine the application, but if I return char no.

If I try to do this to watch what it's return from char:
(format t (string char))
it appears yy (twice???)

Are there some function to convert string to symbols?

Any idea why I need to return the symbol?

I'd like to return char, because char is a variable and no hardcoded.... but I don't understand what's happening

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

Re: #\ vs '

Postby Goheeca » Thu Jan 31, 2013 5:21 am

Code: Select all
(def$method (tty-dialog-mixin :babylon-read) (&optional special-keys)
"reads in a character or a lisp form from dialog-stream.
only those characters occurring in the list special-keys are read."

  (let ((char (read-char dialog-stream)))
    (cond ((member char special-keys :test 'char-equal)   
           (clear-input dialog-stream) char)
          (t (prog2 (unread-char char dialog-stream)
                    (read dialog-stream)
                    (clear-input dialog-stream))))))

You can add the currency sign to the list (special-keys) which is used during the invocation or redefine it like this:
Code: Select all
(def$method (tty-dialog-mixin :babylon-read) (&optional special-keys)
"reads in a character or a lisp form from dialog-stream.
only those characters occurring in the list special-keys are read."

  (let ((char (read-char dialog-stream)))
    (cond ((member char special-keys :test 'char-equal)   
           (clear-input dialog-stream) char)
          ((char= char #\CURRENCY_SIGN) char)
          (t (prog2 (unread-char char dialog-stream)
                    (read dialog-stream)
                    (clear-input dialog-stream))))))

But it's not standard, because the CL standard doesn't define a representation of any unicode char so for a better portability you should use something like:
Code: Select all
(char "¤" 0)

instead of #\CURRENCY_SIGN. Still it's not a CL conformant.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 198
Joined: Thu May 10, 2012 12:54 pm


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 44 guests

cron