LISP is an acronym for list processing, it is one the oldest programming language currently being used in the field of artificial intelligence. It performs its computations on symbolic expressions which makes it too reliable for AI.
File handling is a method by which we can access files and store data in them after executing input and output operations for future references since the data is not lost.
In this article, we will learn the very basics of file manipulation with LISP.
1. Opening a file in LISP:
The most basic task is to read the content of a file. In LISP we have the OPEN function with which we can open an existing file or create a new file.
Syntax:
open filename &key :direction :element-type :if-exists :if-does-not-exist :external-format
In the syntax given above, we have multiple Keyword arguments. We should note that Keyword arguments are helpful in defining the type of stream and different error handling ways.
Parameters:
- Direction: Defines what stream should handle input (default) , :output, :io (both input and output), :probe (checks file existence)
- Element-type: Type of element in the stream.
- If-exists: It is used for checking conditions. Format it can take.
:error - for error signaling.
:rename - for renaming an existing file.
nil - indicates failure.
:rename-and-delete -first rename an existing file then delete it.
:append - Appending to an existing file.
- if-does-not-exist: Triggered when the file to be opened does not exist. Format it can take:
: error - for error signaling.
: create - creates an empty file with the name specified for further use.
nil - indicates failure.
- external-format: Specifies the scheme for character representation in files.
Syntax:
(open "helloworld.file" :direction :output :if-exists :append :if-does-not-exist :create)
After execution of the above command, a file is opened or a new file is created if it does not exist.
2. Reading from and Writing to a file in LISP:
With the help of the WITH-OPEN-FILE macro, a file can be opened for both reading and writing with the automatic closing of the file.
Syntax:
with-open-file (stream filename {options}*) {declaration}* {form}*
- filename: refers to the name of the file to be opened.
- options: keyword arguments.
Example 1:
Lisp
(write-line "Hello World")
; Writing to a file
(with-open-file (file "helloworld.txt" :direction :output
:if-exists :supersede
:if-does-not-exist :create)
; Here :supersede supersedes (replaces) the preexisting file and
; the :direction :output ensures that the file is
; opened for the writing purpose.
(dolist (line '("Banana" "Guava" "Apple" "Cherry" "Mango"
"Strawberry" "Pineapple" "Avocado"
))
(write-line line file)))
; Since with open file is used so file is closed automatically
; Reading from a file
(let ((in (open "helloworld.txt" :if-does-not-exist nil)))
; :if does not exist set to null means there's
; no file to read from so indicate failure
(when in
(loop for line = (read-line in nil)
while line do (format t "~a~%" line))
(close in)
)
)
; stream" in" is closed with close command at last
Output:
3. Closing a file in LISP:
Closing a file in LISP is performed using CLOSE which closes a stream, by closing a stream it is ensured that the stream won't be used for any input or output operation. All the association between stream and file ends with the CLOSE command.
Note: Closing a file with CLOSE is not required with the WITH_OPEN_FILE macro as it is closed automatically.
Example 2 :
Lisp
;Writing to a file
(let ((file (open "helloworld.txt" :direction :output
:if-exists :supersede
:if-does-not-exist :create)))
(dolist (line '("computer network" "DSA" "Data Mining"
"C++" "C#" "MATLAB"))
(write-line line file))
(close file))
;Stream "file" closed with close command at last
; Reading from a file
(let ((in (open "helloworld.txt" :if-does-not-exist nil)))
(when in
(loop for line = (read-line in nil)
while line do (format t "~a~%" line))
(close in)
)
)
; Stream "in" closed with close command at last
Output:
Similar Reads
File Handling in Julia Julia is a high-level, high-performance, and dynamic programming language that supports file Handling i.e., to read and write files. It is much easier for Julia to open, read, and write files as compared to various other languages. It has a similar way to handle the file as compared to python. It pr
6 min read
Error Handling in LISP The condition system in Lisp is one of its best features. It accomplishes the same task as the exception handling mechanisms in Java, Python, and C++ but is more adaptable. In reality, its adaptability goes beyond error handling since conditions, which are more flexible than exceptions, can represen
9 min read
File Handling in Ruby It is a way of processing a file such as creating a new file, reading content in a file, writing content to a file, appending content to a file, renaming the file and deleting the file. Common modes for File Handling "r" : Read-only mode for a file. "r+" : Read-Write mode for a file. "w" : Write-onl
4 min read
Perl | File Handling Introduction In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here's an introduction to file handling in Perl: File modes:When opening a file in Perl, you need to spec
7 min read
Perl | File Handling Introduction In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here's an introduction to file handling in Perl: File modes:When opening a file in Perl, you need to spec
7 min read
Functions in LISP A function is a set of statements that takes some input, performs some tasks, and produces the result. Through functions, we can split up a huge task into many smaller functions. They also help in avoiding the repetition of code as we can call the same function for different inputs. Defining Functio
3 min read