LISP Programming By albro
The Lisp programming language is famous for its very specific approach to code structure and how to work with them. The word "Lisp" is an abbreviation of "List Processing". Lisp is one of the oldest high-level languages in use today. The infrastructure of this language is designed to treat codes as data. This principle has significantly influenced the unique syntax and its programming model. From the Lisp language, several modern versions such as Common Lisp, Emacs Lisp (Elisp), Clojure, Racket, Scheme, Fennel, and GNU Guile have been created. This language has very professional applications that have made programmers still try to teach it.
In this post, I have reviewed Lisp programming language. First, I got acquainted with this language and then I have stated some of its simple coding rules. Finally, I got acquainted with its most famous modern version, Common Lips, and talked about the most important features and advantages of its use. Lisp language maintains its place among professional programmers and is always ready to do big projects.
What is Lisp programming language?
The Lisp programming language is a member of the family of programming languages that uses "Parenthesized Prefix Notation". After Fortran, it is the second oldest high-level programming language that is still widely used. Lisp was developed in 1958 by John McCarthy at MIT. The name of this language, "Lisp" is derived from the term "List Processing". Because linked lists are one of the most important data structures in this language. This structure helps programs to work with their source code as data.
Lisp is known for its unique features. These features have been effective on a large number of languages developed since then. Lisp is a functional programming language designed to perform simple operations on string data. This language has long been one of the popular languages in the field of artificial intelligence (AI) research. Lisp pioneered many computer science ideas such as tree data structures, higher-order functions, recursive concepts, and self-hosting compilers.
Today, the most famous versions of the Lisp language with the purpose of designing general-purpose programs include Common Lisp, Scheme, Racket, and Clojure. Lisp language is usually used in various fields, especially artificial intelligence. In the field of artificial intelligence, the Lisp language has been integrated with symbolic methods such as neural networks and machine learning techniques. Although the details of such operations usually remain confidential. Another field of Lisp activity is in academic environments. Applications related to basic programming to quantum computing can be done with the help of Lisp. In addition to the above, Lisp is also used as a scripting language in embedded systems. Lisp has also found its own uses in small programming teams.
Since the invention of the computer, programming languages came along with it, and over time, with the development of the computer, they also developed. As human life is more integrated with computers, languages also become more diverse and advanced. There were also more and more diverse job opportunities. For this reason, little by little, programming changed from a very exclusive expertise related to engineers of large companies and computer inventors to general knowledge that was taught to people all over the world by universities and schools. Still, every day computers enter more and more people's lives, and therefore programming languages find more general and specialized uses.
Learning how to use Lisp programming language
A large number of programs written in Lisp exist in large codebases and are used for professional projects. So it is smart to get familiar with these codes. People interested in designing programming languages often appreciate Lisp because the syntax and data of Lisp have the same structure. Lisp codes are basically made up of a list that contains other lists. That's why the name of this language is "List Processing".
Although people who focus on the appearance of programming languages often dislike Lisp because it uses too many parentheses, there is a common joke that the word LISP is a portmanteau of the phrase "Lots of Irritating Superfluous Parentheses".
It doesn't matter how we feel about the number of Lisp parents, the amount of Lisp code being used in large "codebases" across industries is exciting. Therefore, it is better to be familiar with the method of writing the codes of this language.
What is the method of installing Lisp programming language?
There are many implementations of the Lisp programming language. Popular open source versions of this language include SBCL, GNU Lisp, and GNU Common Lisp, or GCL. Either of these can be installed using the Lisp language distribution's Package Manager, but in this post I'll be using the 'clisp' version.
To install this language in the Linux distribution of Fedora version, we can write the following code in the terminal and then press the enter button.
$ sudo dnf install clisp
To install this language in the Debian Linux operating system, we can write the following code in the terminal and then press the enter button.
$ sudo apt install clisp
To install this language on macOS, we can use MacPorts or Homebrew. Just type the following code in the terminal and then press the enter button.
$ sudo dnf install clisp
To install the "clisp" version of the Lisp programming language on Windows, we must first download the Lisp installer file and then run it. You can download the Lisp installation file from the Cygwin site and from the source of the lagoon site. After downloading the GCL binary, we install this language.
Although we are using clisp commands, the main ideas presented in this post are relevant to all versions of the Lisp language. If you use other versions of Lisp, such as gcl or sbcl, the instructions for executing the code will change, but everything else will remain the same.
List processing
The smallest executable unit in Lisp source code is called "Expression". The expressions are written as a list. For example, in the box below, I have written the code for a list that includes the operator +
and two integers 1
and 2
.
(+ 1 2)
The code above is a Lisp statement that uses the +
symbol to evaluate the sum function and the two arguments 1
and 2
. We can use this expression and other codes in the interactive Lisp environment of the Common version. Common environment for coding REPL is called. REPL stands for "Read-Eval-Print Loop". If you are familiar with the IDLE coding environment of the Python programming language, you will also be familiar with the REPL environment.
To use the REPL we need to set up Common Lisp.
$ clisp
[1]>
As a REPL prompt, enter a few simple statements below.
[1]> (+ 1 2)
3
[2]> (- 1 2)
-1
[3]> (- 2 1)
1
[4]> (+ 2 3 4)
9
Functions
So far, we have familiarized ourselves with the basic structure of Lisp language statements. Now we can use Lisp functions in a useful way. The print
function receives any argument we provide and displays it in the terminal. While the pprint
function prints it as "Pretty". The added p
comes from the word Pretty. There are other variants of the print
function in Lisp, but pprint
works well in the REPL.
[1]> (pprint "hello world")
"hello world"
[2]>
Programmers can create their own function using the defun
keyword. The defun
keyword requires a name for the function and possible parameters, which the designed function should accept.
[1]> (defun myprinter (s) (pprint s))
MYPRINTER
[2]> (myprinter "hello world")
"hello world"
[3]>
Variables
We can create variables in Lisp using the setf
keyword.
[1]> (setf foo "hello world")
"hello world"
[2]> (pprint foo)
"hello world"
[3]>
Expressions can also be used nested within other expressions. For this, we need to use a structure similar to Pipeline. For example, after calling the string-upcase
function to convert the characters of the variable value to uppercase letters, it is easy to print the contents of the defined variable to the terminal.
[3]> (pprint (string-upcase foo))
"HELLO WORLD"
[4]>
Lisp programming language is a type of languages with automatic typing. This means that we do not need to specify their type when defining variables. By default, Lisp treats variables containing integers as integers. That is, it knows the type of the variable from the value assigned to it.
[1]> (setf foo 2)
[2]> (setf bar 3)
[3]> (+ foo bar)
5
In this language, if we want an integer to be considered a string by the interpreter, we must enclose it with quotation marks.
[4]> (setf foo "2")
"2"
[5]> (setf bar "3")
"3"
[6]> (+ foo bar)
*** - +: "2" is not a number
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead.
ABORT :R2 Abort main loop
Break 1 [7]>
In the simple REPL session above, both variables foo
and bar
are initialized with numbers enclosed in quotes. Therefore, Lisp interprets them as strings. Mathematical operators cannot be used on string data. So the REPL enters the debugger mode. To exit the troubleshooting mode, we must press the "Ctrl+D
" buttons from the keyboard.
Using the typep
function, we can examine the nature of objects. This function checks whether the desired object is of a specific data type or not. In Lisp, the tokens T
and NIL
represent True
and False
, respectively.
[4]> (typep foo 'string)
NIL
[5]> (typep foo 'integer)
T
Using single quotes before String and Integer prevents Lisp from mistakenly treating these keywords as variables.
[6]> (typep foo string)
*** - SYSTEM::READ-EVAL-PRINT: variable STRING has no value
[...]
This method is a shortcut to take care of language expressions, which is usually done using the quote
function.
[7]> (typep foo (quote string))
NIL
[5]> (typep foo (quote integer))
T
Lists
It is very natural to be able to use the list data structure in the Lisp language. In the box below, I have provided a simple representation of the implementation of this structure.
[1]> (setf foo (list "hello" "world"))
("hello" "world")
In addition, lists can be indexed using the nth function. I have shown how to use this function in the code below.
[2]> (nth 0 foo)
"hello"
[3]> (pprint (string-capitalize (nth 1 foo)))
"World"
Exit REPL
To end REPL sessions, it is enough to press the combination of "Ctrl+D
" buttons on the keyboard. You can also use the quit
keyword in Lisp. I have implemented the method of using this word in the box below.
[99]> (quit)
$
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.