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.

main.lisp

; print A
(write 'a)
; terminate printing
(terpri)
; print #\a
(write #\a)
; terminate printing
(terpri)
; print char a
(write-char #\a)
; terminate printing
(terpri)
; error
(write-char 'a)

Output

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

Update the source code file named main.lisp and type the following code in it.

main.lisp

; case-sensitive comparison
; compare and print a vs b
(write (char= #\a #\b))
; terminate printing
(terpri)
; compare and print a vs a
(write (char= #\a #\a))
; terminate printing
(terpri)
; compare and print a vs A
(write (char= #\a #\A))
; terminate printing
(terpri)
   
;case-insensitive comparision
; compare and print a vs A
(write (char-equal #\a #\A))
; terminate printing
(terpri)
; compare and print a vs b
(write (char-equal #\a #\b))
; terminate printing
(terpri)
; check and print a vs b and c
(write (char-lessp #\a #\b #\c))
; terminate printing
(terpri)
; check and print a vs b and c
(write (char-greaterp #\a #\b #\c))

Output

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

NIL
T
NIL
T
NIL
T
NIL
Advertisements