hunterge
hunterge

Reputation: 653

Differentiating between characters and Integers in Lisp

I have a list in Lisp which I am sorting through, and I want to make an if statement that checks if the current object in the list is a character or an integer.

Is there something like:

(if (EQUAL currentObject char)
...)

Or

(if (EQUAL currentObject integer)
...)

That I can use??

Many thanks.

Upvotes: 0

Views: 68

Answers (1)

Dirk
Dirk

Reputation: 31061

There are various ways to determine, which kind of object you have at hand.

(typep EXPR 'character) ;; ==> True, if EXPR evaluates to a character
(characterp EXPR)       ;; ==> ------------ " -----------------------
(typep EXPR 'integer)   ;; ==> True, if EXPR evaluates to an integer
(integerp EXPR)         ;; ==> ------------ " -----------------------

Have a look at the definition of typep, typecase, characterp, ...

(loop
   for element in list
   do (typecase element
        (integer #| element is an integer number ... |#)
        (character #| element is a character object ... |#)
        (t #| element is something not covered above |#)))

For many built-in types, there are predicate functions available, which can be used in to test, whether a particular value is an instance of that type. Often, these predicates are named after their base type's names, with a "p" appended ("stringp", "symbolp", "numberp", "consp", ...)

Upvotes: 3

Related Questions