How to characterp? 
Author Message
 How to characterp?

Show me the light!

If I try:
(characterp #\a) then I get T

How do I do the same for:

(setq temp '(a b c d))
(characterp (car temp)) gives me NIL.

How can I check if the car or my list is a character or not?

Thanks in advance.



Mon, 22 Apr 2002 03:00:00 GMT  
 How to characterp?
Sorry.  Maybe I should have been more clear.

Suppose the input to my function is a list.

I need to know if the car of this list is a character.  How can I check
this?

(defun my-func (list)
  (cond
    ((characterp (car list)) (do-something))
..........

)

But this check for "characterp" gives me false.

Thanks again.


Quote:
> Show me the light!

> If I try:
> (characterp #\a) then I get T

> How do I do the same for:

> (setq temp '(a b c d))
> (characterp (car temp)) gives me NIL.

> How can I check if the car or my list is a character or not?

> Thanks in advance.



Mon, 22 Apr 2002 03:00:00 GMT  
 How to characterp?
Characters and symbols are two different data types.  In this case, #\a is
a character and a is a symbol.  It appears that what you are really asking
is:  How can I check if the car of my list is a symbol whose print name is
a string of length 1?  This is one way to do it:

(defun my-characterp (sym)
  (equal 1 (length (symbol-name sym))))

which gives (under CMU Common Lisp):

* (my-characterp 'abc)

NIL
* (my-characterp 'a)

T
*  (my-characterp (car '(a b c)))

T
* (my-characterp (car '(abc b c)))

NIL
*

my-characterp would have to be modified to handle lists (i.e.
(mycharacterp '(a b c)) won't work).  Using a function such as
my-characterp is a bad idea anyway;  I recommend instead that you
familiarize yourself with the various Lisp data types (e.g. characters,
strings, symbols, etc.) and then determine which data type is best suited
for the problem at hand.

Regards,

Jim Driese

Quote:

> Sorry.  Maybe I should have been more clear.

> Suppose the input to my function is a list.

> I need to know if the car of this list is a character.  How can I check
> this?

> (defun my-func (list)
>   (cond
>     ((characterp (car list)) (do-something))
> ..........

> )

> But this check for "characterp" gives me false.

> Thanks again.



> > Show me the light!

> > If I try:
> > (characterp #\a) then I get T

> > How do I do the same for:

> > (setq temp '(a b c d))
> > (characterp (car temp)) gives me NIL.

> > How can I check if the car or my list is a character or not?

> > Thanks in advance.



Mon, 22 Apr 2002 03:00:00 GMT  
 How to characterp?

Quote:

> Show me the light!

*fwap* (hits divya on the head with a lamp)

Quote:
> If I try:
> (characterp #\a) then I get T

> How do I do the same for:

> (setq temp '(a b c d))
> (characterp (car temp)) gives me NIL.

> How can I check if the car or my list is a character or not?

You just did.

(a b c d) is a list of SYMBOLS.

(#\a #\b #\c #\d) is a list of CHARACTERS.

-matt



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?

| Show me the light!

* R. Matthew Emerson
| *fwap* (hits divya on the head with a lamp)

  hmmm...  "lampda -- the ultimate source of light"

#:Erik, who fears he's about to be hit, too :)



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?
I read your recent message too. But I thought your recent message
neither has the clue.

characterp will return a true(T) or false(NIL) value.

In your example, a in '(a b c d) is not a character! It's called
"Symbol" so characterp
returns nil.

If you try your example with '(#\a #\b #\c #\d) you will get true when
you call characterp
with the list.

- jwc

Quote:

> Show me the light!

> If I try:
> (characterp #\a) then I get T

> How do I do the same for:

> (setq temp '(a b c d))
> (characterp (car temp)) gives me NIL.

> How can I check if the car or my list is a character or not?

> Thanks in advance.



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?

Quote:

> Show me the light!
> If I try:
> (characterp #\a) then I get T
> How do I do the same for:
> (setq temp '(a b c d))
> (characterp (car temp)) gives me NIL.
> How can I check if the car or my list is a character or not?

Like that.  It's not a character, it's a symbol!

        (characterp (car '(#\a #\b))) -> T

--tim



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?

| I need to know if the car of this list is a character.  How can I check
| this?

  with CHARACTERP or (TYPEP <whatever> 'CHARACTER).

| But this check for "characterp" gives me false.

  then you don't have a character.  how hard can this be to understand?

  why don't you ask your real question, which ought to be very close to
  "what are characters in Common Lisp?"

#:Erik



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?

: I need to know if the car of this list is a character.  How can I check
: this?
:
: (defun my-func (list)
:   (cond
:     ((characterp (car list)) (do-something))

That function actually does what you _say_ you want it to do. That is,
if LIST is '(#\c) then

   (characterp (car list))

will return T.

Be sure you understand the difference between a character and a symbol
in Lisp parlance. If you want your test to give T when LIST is '(c),
then you are not looking for characters:

   (characterp #\c) => T
   (characterp 'c) => NIL
   (symbolp #\c) => NIL
   (symbolp 'c) => T

   (setq x #\c) => #\c
   (characterp x) => T
   (symbolp x) => NIL

   (setq y 'c) => C
   (characterp y) => NIL
   (symbolp y) => T

But take into account that:

   (symbolp 'i-am-a-symbol) => T

Also, a hint on style: if the flow depends on a unique predicate as in
your code, WHEN is the natural choice.

-- Shin



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?
Thank you all for your answers.

I received some critique from some smart-asses too.  I don't think
I am doing bad for a 2 day old LISP programmer and I will ignore
this critique from, probably LISP programmers of 40 years, for whom
the basic concepts are second nature.

Divya


Quote:
> Sorry.  Maybe I should have been more clear.

> Suppose the input to my function is a list.

> I need to know if the car of this list is a character.  How can I check
> this?

> (defun my-func (list)
>   (cond
>     ((characterp (car list)) (do-something))
> ..........

> )

> But this check for "characterp" gives me false.

> Thanks again.



> > Show me the light!

> > If I try:
> > (characterp #\a) then I get T

> > How do I do the same for:

> > (setq temp '(a b c d))
> > (characterp (car temp)) gives me NIL.

> > How can I check if the car or my list is a character or not?

> > Thanks in advance.



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?
Hey Eric!

I am getting jelaous now! Please shoot at me! AT ME! You do need anybody
else when you have ME!

But to be serious: It is probably possible to alter the READER so that I can
define, what a "charcter" means. Hm?



Quote:

> | I need to know if the car of this list is a character.  How can I check
> | this?

>   with CHARACTERP or (TYPEP <whatever> 'CHARACTER).

> | But this check for "characterp" gives me false.

>   then you don't have a character.  how hard can this be to understand?

>   why don't you ask your real question, which ought to be very close to
>   "what are characters in Common Lisp?"

> #:Erik



Tue, 23 Apr 2002 03:00:00 GMT  
 How to characterp?
* Janos Blazi
| Hey Eric!


  posted on 1999-11-03 01:40:55+01, contained your promise to _end_ your
  particular line of lunatic ravings, and I answered your questions in the
  spirit of that promise.  you promised, and I quote your very words:

| But all right.  This is my last response to you.  You may response once
| and I shall read your response and I shall not answer any more.  So
| please do not talk to me again (bur for your last response).

  I am not particularly surprised that you can't even keep simple promises,
  but can you AT THE VERY LEAST PLEASE SHUT UP?

#:Erik



Wed, 24 Apr 2002 03:00:00 GMT  
 How to characterp?
I am very sorry but I think that you answered my posting on Russian mental
power AFTER our "final" messages. And my posting about Russian mental power
had really nothing to with you and was not in any way offending you.

So it was you who started again. But I did not mind. I admit I was angry of
you but I am not angry any more. I have forgiven you, dear Eric.

J.B.



Quote:
> * Janos Blazi
> | Hey Eric!

>   sigh.


Quote:
>   posted on 1999-11-03 01:40:55+01, contained your promise to _end_ your
>   particular line of lunatic ravings, and I answered your questions in the
>   spirit of that promise.  you promised, and I quote your very words:

> | But all right.  This is my last response to you.  You may response once
> | and I shall read your response and I shall not answer any more.  So
> | please do not talk to me again (bur for your last response).

>   I am not particularly surprised that you can't even keep simple
promises,
>   but can you AT THE VERY LEAST PLEASE SHUT UP?

> #:Erik



Wed, 24 Apr 2002 03:00:00 GMT  
 How to characterp?

Quote:

> Thank you all for your answers.

> I received some critique from some smart-asses too.  I don't think
> I am doing bad for a 2 day old LISP programmer and I will ignore
> this critique from, probably LISP programmers of 40 years, for whom
> the basic concepts are second nature.

No biggie.  Symbols are not the most intutive thing for someone
moving from other computer languages.  You're doing fine.

dave



Fri, 26 Apr 2002 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software