
- LISP Tutorial
- LISP - Home
- LISP - Overview
- LISP - Environment
- LISP - REPL
- LISP - Program Structure
- LISP - Basic Syntax
- LISP - Data Types
- Lisp Macros
- LISP - Macros
- LISP - Backquote and Comma
- LISP - Code Generation Using Macro
- LISP - Variable Capture and Hygienic macro
- LISP - Scope and Binding
- LISP - Macro Writing Style
- LISP - Macro Characters
- LISP - Read-Time Macros
- LISP - Compiler Macros
- LISP - Uses of Macros
- Lisp Functions
- LISP - Functions
- LISP - Functions vs Macros
- LISP - Calling Function using funcall
- LISP - Calling Function using apply
- LISP - Closures
- LISP - Functions as Arguments
- LISP - Functions as Return Values
- LISP - Recursion
- LISP - Built-in Functions
- Lisp Predicates
- LISP - Predicates
- LISP - Generic Data Type Predicates
- LISP - Specific Data Type Predicates
- LISP - Equality Predicates
- LISP - Numeric Predicates
- LISP - Comparison Predicates
- LISP - Logical Predicates
- LISP - List Predicates
- LISP - Custom Predicates
- LISP - Chaining Predicates
- Lisp Arrays
- LISP - Arrays
- LISP - Adjustable Arrays
- LISP - Fill Pointers in Arrays
- LISP - Specialized Arrays
- LISP - Arrays Properties
- LISP - Iterating over Arrays
- LISP - Multidimensional Arrays
- LISP - Row-Major Order
- Lisp Strings
- LISP - Strings
- LISP - String Concatenation
- LISP - String Comparison
- LISP - String Case Conversion
- LISP - String Trimmimg
- LISP - String Searching
- LISP - Getting Substring
- LISP - String Replacement
- LISP - Sorting Strings
- LISP - Merging Strings
- LISP - Accessing Characters of String
- LISP - String length
- LISP - Escape Sequences
- Lisp Sequences
- LISP - Sequences
- LISP - Accessing Element of Sequence
- LISP - Sequence length
- LISP - Getting Subsequence
- LISP - Search Element in Sequence
- LISP - Sequence Concatenation
- LISP - Reversing a Sequence
- LISP - Mapping Sequence Element
- LISP - position of Element
- LISP - Remove an Element
- LISP - Sort Sequence
- LISP - Merge Sequences
- LISP - every function
- LISP - some function
- LISP - notany function
- LISP - notevery function
- Lisp Lists
- LISP - Lists
- LISP - Accessing Elements of Lists
- LISP - Modifications to Lists
- LISP - Using mapcar on List
- LISP - Using mapc on List
- LISP - Using reduce on List
- LISP - Removing elements from List
- LISP - Reversing a List
- LISP - Sorting a List
- LISP - Searching a List
- LISP - List vs Vectors
- LISP - Matrix Multiplication
- Lisp Vectors
- LISP - Vectors
- LISP - Creating Vectors
- LISP - Accessing Elements of Vectors
- LISP - Modifications to Vectors
- LISP - Adjustable Vectors
- LISP - Specialized Vectors
- LISP - Vector Functions
- Lisp Set
- LISP - Set
- LISP - Adding elements to the Set
- LISP - Getting SubSet from a Set
- LISP - Set Difference
- LISP - Set Exclusive OR
- LISP - Set Intersection
- LISP - Set Union
- LISP - Representing Set with HashTable
- LISP - List as Set vs HashTable as Set
- Lisp Tree
- LISP - Tree
- LISP - Recursive Traversal
- LISP - Inorder Traversal
- LISP - Preorder Traversal
- LISP - Postorder Traversal
- LISP - Depth First Traversal
- LISP - Modifying Tree
- LISP - Search Tree
- LISP - Binary Tree
- Lisp Hash Table
- LISP - Hash Table
- Adding Values to Hash Table
- Removing Values from Hash Table
- Updating Values of Hash Table
- Iterating Hash Table Entries
- Searching key in HashTable
- Checking Size of HashTable
- Using Custom Equality Check
- Lisp - Input − Output
- LISP - Input − Output
- LISP - Streams
- LISP - Reading Data from Streams
- LISP - Writing Data to Streams
- LISP - File I/O
- LISP - String I/O
- LISP - Formatting with Format
- LISP - Interactive I/O
- LISP - Error Handling
- LISP - Binary I/O
- Lisp - Structures
- LISP - Structures
- LISP - Accessors and Mutators
- LISP - Structure Options
- LISP - Structure Types
- LISP - Applications and Best Practices
- Lisp - CLOS
- LISP - CLOS
- Lisp - Objects
- LISP - Class
- LISP - Slots and Accessors
- LISP - Generic Functions
- LISP - Class Precedence
- LISP - Metaobject Protocol
- LISP - Multimethods
- LISP - Multiple Inheritance
- LISP - Method Combinations
- LISP - Method Combinations
- LISP - :before Method Combination
- LISP - :primary Method Combination
- LISP - :after Method Combination
- LISP - :around Method Combination
- LISP - + Method Combination
- LISP - and Method Combination
- LISP - append Method Combination
- LISP Useful Resources
- Lisp - Quick Guide
- Lisp - Useful Resources
- Lisp - Discussion
Lisp - Strings
Strings in Common Lisp are vectors, i.e., one-dimensional array of characters.
String literals are enclosed in double quotes. Any character supported by the character set can be enclosed within double quotes to make a string, except the double quote character (") and the escape character (\). However, you can include these by escaping them with a backslash (\).
Example
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; print statements (write-line "Hello World") (write-line "Welcome to Tutorials Point") ; escaping the double quote character (write-line "Welcome to \"Tutorials Point\"")
Output
When you execute the code, it returns the following result −
Hello World Welcome to Tutorials Point Welcome to "Tutorials Point"
String Comparison Functions
Numeric comparison functions and operators, like, < and > do not work on strings. Common LISP provides other two sets of functions for comparing strings 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 |
---|---|---|
string= | string-equal | Checks if the values of the operands are all equal or not, if yes then condition becomes true. |
string/= | string-not-equal | Checks if the values of the operands are all different or not, if values are not equal then condition becomes true. |
string< | string-lessp | Checks if the values of the operands are monotonically decreasing. |
string> | string-greaterp | Checks if the values of the operands are monotonically increasing. |
string<= | string-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. |
string>= | string-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 for equality (write (string= "this is test" "This is test")) ; terminate printing (terpri) ; case-sensitive comparison for greater than (write (string> "this is test" "This is test")) ; terminate printing (terpri) ; case-sensitive comparison for less than (write (string< "this is test" "This is test")) ; terminate printing (terpri) ;case-insensitive comparision for equality (write (string-equal "this is test" "This is test")) ; terminate printing (terpri) ;case-insensitive comparision for greater than (write (string-greaterp "this is test" "This is test")) ; terminate printing (terpri) ;case-insensitive comparision for less than (write (string-lessp "this is test" "This is test")) ; terminate printing (terpri) ; checking non-equality (write (string/= "this is test" "this is Test")) ; terminate printing (terpri) (write (string-not-equal "this is test" "This is test")) ; terminate printing (terpri) (write (string/= "lisp" "lisping")) ; terminate printing (terpri) (write (string/= "decent" "decency"))
Output
When you execute the code, it returns the following result −
NIL 0 NIL T NIL NIL 8 NIL 4 5
Case Controlling Functions
The following table describes the case controlling functions −
Sr.No. | Function & Description |
---|---|
1 |
string-upcase Converts the string to upper case |
2 |
string-downcase Converts the string to lower case |
3 |
string-capitalize Capitalizes each word in the string |
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; convert to upper case and print statement (write-line (string-upcase "a big hello from tutorials point")) ; convert to Capital case and print statement (write-line (string-capitalize "a big hello from tutorials point"))
Output
When you execute the code, it returns the following result −
A BIG HELLO FROM TUTORIALS POINT A Big Hello From Tutorials Point
Trimming Strings
The following table describes the string trimming functions −
Sr.No. | Function & Description |
---|---|
1 |
string-trim It takes a string of character(s) as first argument and a string as the second argument and returns a substring where all characters that are in the first argument are removed off the argument string. |
2 |
String-left-trim It takes a string of character(s) as first argument and a string as the second argument and returns a substring where all characters that are in the first argument are removed off the beginning of the argument string. |
3 |
String-right-trim It takes a string character(s) as first argument and a string as the second argument and returns a substring where all characters that are in the first argument are removed off the end of the argument string. |
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; trim and print the statement (write-line (string-trim " " " a big hello from tutorials point ")) ; trim spaces on left and print the statement (write-line (string-left-trim " " " a big hello from tutorials point ")) ; trim spaces on right and print the statement (write-line (string-right-trim " " " a big hello from tutorials point ")) ; trim spaces on both left and right and print the statement (write-line (string-trim " a" " a big hello from tutorials point "))
Output
When you execute the code, it returns the following result −
a big hello from tutorials point a big hello from tutorials point a big hello from tutorials point big hello from tutorials point
Other String Functions
Strings in LISP are arrays and thus also sequences. We will cover these data types in coming tutorials. All functions that are applicable to arrays and sequences also apply to strings. However, we will demonstrate some commonly used functions using various examples.
Calculating Length
The length function calculates the length of a string.
Extracting Sub-string
The subseq function returns a sub-string (as a string is also a sequence) starting at a particular index and continuing to a particular ending index or the end of the string.
Accessing a Character in a String
The char function allows accessing individual characters of a string.
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; print length of the statement (write (length "Hello World")) ; terminate printing (terpri) ; print substring of statement starting from index 6 (write-line (subseq "Hello World" 6)) ; print char from string at index 6 (write (char "Hello World" 6))
Output
When you execute the code, it returns the following result −
11 World #\W
Sorting and Merging of Strings
The sort function allows sorting a string. It takes a sequence (vector or string) and a two-argument predicate and returns a sorted version of the sequence.
The merge function takes two sequences and a predicate and returns a sequence produced by merging the two sequences, according to the predicate.
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
;sorting the strings (write (sort (vector "Amal" "Akbar" "Anthony") #'string<)) ; terminate printing (terpri) ;merging the strings ; merge sequences and print (write (merge 'vector (vector "Rishi" "Zara" "Priyanka") (vector "Anju" "Anuj" "Avni") #'string<))
Output
When you execute the code, it returns the following result −
#("Akbar" "Amal" "Anthony") #("Anju" "Anuj" "Avni" "Rishi" "Zara" "Priyanka")
Reversing a String
The reverse function reverses a string.
For example, update the source code file named main.lisp and type the following code in it.
main.lisp
; print reverse of the statement (write-line (reverse "Are we not drawn onward, we few, drawn onward to new era"))
Output
When you execute the code, it returns the following result −
are wen ot drawno nward ,wef ew ,drawno nward ton ew erA
Concatenating Strings
The concatenate function concatenates two strings. This is generic sequence function and you must provide the result type as the first argument.
For example, update the source code file named main.lisp and type the following code in it.
main.lisp
; concatenate and print statements (write-line (concatenate 'string "Are we not drawn onward, " "we few, drawn onward to new era"))
Output
When you execute the code, it returns the following result −
Are we not drawn onward, we few, drawn onward to new era