Page 1 of 1

#\ vs '

Posted: Wed Jan 30, 2013 12:11 pm
by santiago
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

Re: #\ vs '

Posted: Wed Jan 30, 2013 12:32 pm
by Goheeca
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 \#?

Re: #\ vs '

Posted: Wed Jan 30, 2013 5:47 pm
by edgar-rft
\# 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

Re: #\ vs '

Posted: Thu Jan 31, 2013 12:57 am
by santiago
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

Re: #\ vs '

Posted: Thu Jan 31, 2013 3:00 am
by JamesF
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.

Re: #\ vs '

Posted: Thu Jan 31, 2013 3:36 am
by santiago
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

Re: #\ vs '

Posted: Thu Jan 31, 2013 5:21 am
by Goheeca

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.