Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to characterp?

8 views
Skip to first unread message

Divya

unread,
Nov 4, 1999, 3:00:00 AM11/4/99
to
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.

Divya

unread,
Nov 4, 1999, 3:00:00 AM11/4/99
to
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.


Divya <ra...@frontiernet.net> wrote in message
news:7vtie4$15lg$1...@node17.cwnet.frontiernet.net...

Jim Driese

unread,
Nov 4, 1999, 3:00:00 AM11/4/99
to
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

R. Matthew Emerson

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
"Divya" <ra...@frontiernet.net> writes:

> Show me the light!

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

> 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

Erik Naggum

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
* Divya <ra...@frontiernet.net>
| 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 :)

Jong-won Choi

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
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

Divya wrote:
>
> 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.

Tim Bradshaw

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
* Divya wrote:
> 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


Erik Naggum

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
* "Divya" <ra...@frontiernet.net>

| 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

Shin

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
On Thu, 4 Nov 1999 23:18:21 -0500, "Divya" <ra...@frontiernet.net> wrote:

: 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

Divya

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
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

Divya <ra...@frontiernet.net> wrote in message

news:7vtlom$118c$1...@node17.cwnet.frontiernet.net...


> 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.
>
>
> Divya <ra...@frontiernet.net> wrote in message
> news:7vtie4$15lg$1...@node17.cwnet.frontiernet.net...

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

Janos Blazi

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
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?

Erik Naggum <er...@naggum.no> schrieb in im Newsbeitrag:
31507978...@naggum.no...
> * "Divya" <ra...@frontiernet.net>


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

Erik Naggum

unread,
Nov 6, 1999, 3:00:00 AM11/6/99
to
* Janos Blazi
| Hey Eric!

sigh. <capitoline-7vo0b2/INN-2.2.1/acc...@broadway.news.is-europe.net>,
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

Janos Blazi

unread,
Nov 6, 1999, 3:00:00 AM11/6/99
to
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.


Erik Naggum <er...@naggum.no> schrieb in im Newsbeitrag:

31508838...@naggum.no...

David Hanley

unread,
Nov 8, 1999, 3:00:00 AM11/8/99
to

Divya wrote:

> 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


0 new messages