Docsity

Evaluation of S Expression - Programming Languages - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Evaluation of S Expression - Programming Languages - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

LISP

  • Invented by John McCarthy (1958)
  • Two simple data structure (atoms and lists)
  • Heavy use of recursion
  • Interpretive language
  • Variations
    • Scheme
    • Common Lisp (de facto industrial standard)
  • Most widely used AI programming language
  • Functional Programming Paradigm
  • Low maintenance overhead
  • Atoms may be;
    • Numbers : (real 1.0, integer 1)
    • Symbols : a consecutive sequence of characters (no space) e.g. a, x, price-of-beef
    • Two special symbols: T and NIL for logical true and false
    • Strings : a sequence of characters bounded by double quotes e.g. "this is red"

Valid Objects

  • Its also a list
  • Uses prefix notation:

(function-name arg1,arg2 ... argN)

  • It returns function value for the given list of arguments
  • Functions are either provided by LISP function library or defined by the user

Function Calls

  • Examples:

(+ 1 3 5) 9

(/ 3 5) 3/

(/ 3.0 5)

(sqrt 4) 2

Function Calls

Command Line Prompt

Evaluation of S-expression

2) Evaluate a list - evaluate every top element of the list

as follows, unless explicitly forbidden:

  • The first element is always a function name;

evaluating it means to call the function body

  • Each of the rest elements will then be evaluated, and

their values returned as the arguments for the

function

  • Examples :

> (sqrt x) Error: The variable X is unbound

> (+ (sqrt 4) 4.0)

Evaluation of S-expression

> (setq y x)

> y

> (+ x y)

  • To forbid evaluation of a symbol use : quote or ’

(quote x) x

'x x

(setq z 'x) x

Functions (cont’)

• Selectors

  • first, rest, nth, car, cdr…

> (first '(a s d f)) ;returns the first element of a list

a

> (rest '((a s) d f)) ;deletes the first element from the

;list and returns the rest as a list

(d f)

> (first '((a s) d f))

(a s)

> (rest '((a s) (d f))

((d f)) Docsity.com

> (setq L '(A B C))

(A B C)

> (car L)

; returns the first top level element of list L

A

> (cdr L)

; returns the rest of list L

(B C)