Open In App

Program Structure in LISP

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The expressions we use in Lisp are known as the s-expressions which are composed of atoms, strings, and lists. An s-expression is itself a valid program.

REPL:

REPL is an acronym for Read Evaluate Print Loop. In the case of an interpreter, the program is read, checked for errors in loops, and the value is returned by the program.

Lisp program can run either on an interpreter or as a compiled code.

Simple Program in Lisp:

Lisp




; LISP code for
; Multiplication of 4 number in Lisp
 
(write (* 2 5 8 10))


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

Output:

 

Prefix Notation in Lisp:

  • Note that in prefix notation the operators are written before the operands.
  • For any operation to be performed, prefix notation is used as in the above example.
  • The ‘*’ symbol above acts as a function that multiplies the numbers together provided as the operands.

((a+(b-c))/d) in prefix notation is written as:

 (/(+ a (- b c) ) d)

Example: 

Lisp




; LISP code for Expression
; ((9+(6-3))/3)
 
 
(write (/(+ 9(- 6 3))3))


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

Output:

 

Evaluation of Lisp Program: 

Evaluation of the Lisp program involves two steps:

  1. Reader Program translates program text to Lisp objects
  2. Evaluator Program implements language semantics in terms of the objects.

Let us look at the steps of evaluation:

  1. Reader translates program text to s-expression.
  2. The evaluator defines the syntax of the Lisp forms which are built from s-expressions and also determines which s-expressions are Lisp forms.
  3. The evaluator works as a function that takes a valid Lisp form as an argument and evaluates it to a value hence we put the Lisp expression under parenthesis because we send the entire text expression as an argument.

Now to begin writing code in Lisp just create a new file as “name.lisp” and write the mentioned code in it:

Example: 

Lisp




; LISP code for
; Hello World Program
 
(write-line "Hello World")


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

Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads