Characters in LISP

Last Updated : 10 Mar, 2023

In Lisp data objects of type 'character' are referred to as characters. For representation purposes, we usually denote character objects by preceding a #\ symbol before the character. Any character can be represented by using the #\ symbol before the name of the character. For Example #\a represents a character 'a'.

Example 1:

Lisp
;Lisp Code to print characters  

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

Output:

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Note: "terpri" is a command in Lisp that means "terminate printing" as it is used to terminate a line of output without which the output would be printed in one line.

Special Characters in Lisp:

There are some predefined special characters in Lisp which are:

  1. #\Backspace
  2. #\Tab
  3. #\Page
  4. #\Linefeed
  5. #\Return
  6. #\Rubout

Character Comparison Functions:

In Lisp programming we don't use numeric comparison functions rather we make use of character comparing functions. 

There are two sets of character comparing functions:

  • Case-Sensitive Functions: Used when characters are having the same case(lower or upper).
  • Case-Insensitive Functions: Used when characters may be having different cases(lower or upper).
S.No.Case Sensitive FunctionsCase-Insensitive FunctionsDescription
1char=char-equalChecks if operands are equal or not
2char/=char-not-equalChecks if operands are different or not
3char<=char-not-greaterpChecks if the value of the left operand is greater than or equal to the value of the next right operand
4char>=char-not-lesspChecks if the value of the left operand is less than or equal to the value of the next right operand
5char<char-lesspChecks if the values of the operands are monotonically decreasing or not
6char>char-greaterpChecks if the values of the operands are monotonically increasing or not

Note: If in any of the above-mentioned cases the condition in description is satisfied, then it returns 'T' else returns 'NIL'.

Example 2: 

Lisp
;Lisp Case-Sensitive Comparison
(write (char= #\b #\b))
(terpri)
(write (char= #\a #\b))
(terpri)
(write (char= #\A #\c))
(terpri)
(write (char-lessp #\x #\y #\z))
(terpri)
(write (char-greaterp #\a #\b #\c))
(terpri)
   
;Case-Insensitive Comparison
(write (char-equal #\a #\A))
(terpri)
(write (char-equal #\a #\b))
(terpri)
(write (char-lessp #\x #\y #\z))
(terpri)
(write (char-greaterp #\a #\b #\c))

Output:

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment