LISP Tutorial on LISP Characters

in lisp, characters are represented as data objects of type character.

you can denote a character object preceding #\ before the character itself. for example, #\a means the character a.

space and other special characters can be denoted by preceding #\ before the name of the character. for example, #\space represents the space character.

the following example demonstrates this −

example

create a new source code file named main.lisp and type the following code in it.

(write 'a)
(terpri)
(write #\a)
(terpri)
(write-char #\a)
(terpri)
(write-char 'a)

when you execute the code, it returns the following result −

a
#\a
a
*** - write-char: argument a is not a character

special characters

common lisp allows using the following special characters in your code. they are called the semi-standard characters.

  • #\backspace
  • #\tab
  • #\linefeed
  • #\page
  • #\return
  • #\rubout

character comparison functions

numeric comparison functions and operators, like, < and > do not work on characters. common lisp provides other two sets of functions for comparing characters in your code.

one set is case-sensitive and the other case-insensitive.

the following table provides the functions −

case sensitive functions case-insensitive functions description
char= char-equal checks if the values of the operands are all equal or not, if yes then condition becomes true.
char/= char-not-equal checks if the values of the operands are all different or not, if values are not equal then condition becomes true.
char< char-lessp checks if the values of the operands are monotonically decreasing.
char> char-greaterp checks if the values of the operands are monotonically increasing.
char<= char-not-greaterp checks if the value of any left operand is greater than or equal to the value of next right operand, if yes then condition becomes true.
char>= char-not-lessp checks if the value of any left operand is less than or equal to the value of its right operand, if yes then condition becomes true.

example

create a new source code file named main.lisp and type the following code in it.

; case-sensitive comparison
(write (char= #\a #\b))
(terpri)
(write (char= #\a #\a))
(terpri)
(write (char= #\a #\a))
(terpri)
   
;case-insensitive comparision
(write (char-equal #\a #\a))
(terpri)
(write (char-equal #\a #\b))
(terpri)
(write (char-lessp #\a #\b #\c))
(terpri)
(write (char-greaterp #\a #\b #\c))

when you execute the code, it returns the following result −

nil
t
nil
t
nil
t
nil