Basic Commands of LISP, Exams of Programming Languages

commands of lisp programming language cheers

Typology: Exams

2019/2020

Uploaded on 09/03/2020

karanpatil
karanpatil 🇮🇳

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LISP: LISt Processing Language
An AI language developed in 1958 (J. McCarthy at MIT)
• Special focus on symbolic processing and symbol manipulation
– Linked list structures
– Also programs, functions are represented as lists
• At one point special LISP computers with basic LISP functions implemented directly on hardware
were available (Symbolics Inc., 80s)
There are two compiler to compile LISP code:
SBCL: Steel Bank Common Lisp
Clisp: Common LISt Processing language
SBCL: Steel Bank Common Lisp (SBCL) is a high performance Common Lisp compiler.
To install SBCL :
mitwpu@C04L0815:~$ sudo apt-get install sbcl
To start SBCL go to terminal and type sbcl and press enter.
$ sbcl
Afterwards * (star symbol) will appear then start to type the commands.
eg. *(set ' (a b c))
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Basic Commands of LISP and more Exams Programming Languages in PDF only on Docsity!

LISP: LISt Processing Language

- An AI language developed in 1958 (J. McCarthy at MIT)

  • Special focus on symbolic processing and symbol manipulation
  • Linked list structures
  • Also programs, functions are represented as lists
  • At one point special LISP computers with basic LISP functions implemented directly on hardware were available (Symbolics Inc., 80s) There are two compiler to compile LISP code:  SBCL: Steel Bank Common Lisp  Clisp: Common LISt Processing language  SBCL: Steel Bank Common Lisp ( SBCL ) is a high performance Common Lisp compiler. To install SBCL : mitwpu@C04L0815:~$ sudo apt-get install sbcl To start SBCL go to terminal and type sbcl and press enter. $ sbcl Afterwards * (star symbol) will appear then start to type the commands. eg. *(set ' (a b c))

Basic LISP Commands: Create list in LISP and perform various commands: List1: sub (CO, PPL, DELD, OOP, Math) List2: marks (10, 20, 30, 40)  set  setf  setq  car (first)  cdr (rest)  reverse  append  cons  length  nth  nthcdr  butlast  last  User defined Functions Note: sub and marks these are the names of set which are created using set command. All other commands are performed on both the sets.

 setf: setf assigns the result of evaluating to the location specified in the immediately

preceding . It returns the result of evaluating the last . If no - pairs are specified, setf returns nil.

syntax:

(setf

. . ) : either (i) the name of a variable, or (ii) an expression referring to part of a larger structure (e.g. a list, property list, structure, or array). : any LISP expression. eg.

  • (setf sub '(ppl oop co deld)) (PPL OOP CO DELD)
  • sub (PPL OOP CO DELD)

 car/first: It takes a list as argument, and returns its first element from the list or set.

syntax:

(car ) / (first )

eg.

*(car ‘(a b c d e f))) A ;output *(first '(a)) A ;output *(car ‘(PPL CO DELD)) PPL ;output

  • (car sub) ;sub is the name of set which one is declared earlier PPL
  • (car marks) ; marks is the name of set which one is declared earlier. 10
  • (first sub) PPL
  • (car (cdr sub)) OOP

 cdr/rest: It takes a list as argument, and returns a list without the first element.

syntax:

(cdr ) / (rest )

eg.

*(cdr ‘(a b c d e f))) (B C D E F) ;output *(rest '(a)) Nill

  • (cdr marks) (20 0 40)
  • (rest sub) (OOP CO DELD)

 cons: It takes two arguments, an element and a list and returns a list with the element inserted at

the first place.

syntax:

(cons )

eg.

* (cons ‘a ‘(b c)))

(A B C) ;output

***** (cons sub marks) ((PPL OOP CO DELD) 10 20 30 40)

 length: It returns length of the list.

syntax: (length ) eg. *(length '(1 2 3 4 5 6 7)) 7 ;output

  • (length (cons sub marks)) 5
  • (length sub) 4
  • (length marks) 4

 nth: It returns the Nth element of the list.

syntax: (nth ) eg. *(nth 0 '(p r w c)) P ;output

*(nth 2 '(p r w c)) W

  • (nth 0 sub) PPL

 nthcdr: The nthcdr function is associated with the cdr function.

syntax:

(nthcdr )

eg.

  • (nthcdr 1 sub) (OOP CO DELD) ;output
  • (nthcdr 2 sub) (CO DELD)
  • sub (PPL OOP CO DELD)

 butlast: If butlast is used with a single argument then ,it will return the list argument with the last

element removed and if butlast is given an integer second argument then it will remove the number of elements specified from the end of the list.

syntax:

(butlast ) (butlast )

eg.

  • (butlast '(a s d f)) (A S D) ;output
  • (butlast '(a s d f) 2)

 Defining user define Functions in LISP: Defun

Use defun to define user define functions in LISP. Defun requires you to provide three things.  the first is the name of the function ,  the second is a list of parameters for the function, and  the third is the body of the function -- i.e. LISP instructions that tell the interpreter what to do when the function is called Definition of a function: Syntax: (defun ) ; a LISP function for Addition of two numbers

  • (defun add(n1 n2) ( + n1 n2)) ADD
  • (add 2 3) 5 ;output ; a LISP function to calculate radius of the circle
  • (defun cir(r) ( * 3.14 r r)) CIR
  • (cir 2) 12.56 ;output ; a LISP function to calculate the square of a number (defun square (x) ( x x)) SQUARE *(square 2) 4 ;output

; a LISP function to calculate the factorial of number (defun factorial (N) "Compute the factorial of N." (if (= N 1) 1 (* N (factorial (- N 1))))) (write (factorial 6)) 720 ;output ; a LISP function which returns the maximum of three numbers (defun maxthree(n1 n2 n3) (cond ((> n1 n2) (cond ((> n1 n3) n1) (t n3) ) ) ((> n2 n3) n2) (t n3) ) ) (princ (maxthree 45 64 9)) 64 ;output  To execute the .lisp file on sbcl ,use command load and .lisp file name square.lisp containing this below mentioned code: (defun sr(n) ( * n n)) Syntax: (load “< .lisp file name>”) It will return T as a true

  • (load "square.lisp") T