Lisp

1 / 26

Lisp - PowerPoint PPT Presentation

Lisp. Functional Language or Applicative Language Achieves its effect by applying functions, either recursively or through composition Powerful, expressive, and semantically elegant language http://www.youtube.com/watch?v=D5kIq5dyb6o&amp;NR=1. Pure Functions. Pure functions avoid “side effects”

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about 'Lisp' - jacob

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lisp
• Functional Language or Applicative Language
• Achieves its effect by applying functions, either recursively or through composition
• Powerful, expressive, and semantically elegant language

Pure Functions
• Pure functions avoid “side effects”
• No use of global variables
• Do not change parameters
• Return one thing
• Obeys principle of referential transparency

“Function using same arguments will always produce same value”

Function examples

plustwo(x) ::= plusone (plusone(x))

where plusone(x) ::= x+1

fact(n) ::= if n = 0 then 1

else n * fact(n-1)

Lisp’s base language
• Pure functions, operations, functional composition, recursion, and conditional expression
• Basics of Lisp
• Domain language operates on S-expr
• S-expr constructed out of
• atoms
• and parentheses (list of atoms)
Basics…
• Atom (primitive data structure)
• Indivisible
• Numeric, non-numeric (some versions string)
• Sequence of alphabetic, numeric characters
• 876 -250 ATOM four
• Equality is only built-in operation
• S-expr
• Atom: 876 four
• List: (ADD 2 PLUS 3)

((Ruth a) (Edward b))

(TIMES Y Z)

Lists and Operators
• Lists can be versatile
• Can be composed of constants, variables, and operators
• Built- in binary functions for arithmetic exist

(plus 2 3) or (+ 2 3)

(+ 2 3 7 8 9 )

Conditions

(cond

( (null x) 0)

( (eq x y) (f x) )

( T (g y) ) )

if null (x)

then 0

elseif x == y then f(x)

else g(y)

endif;

How to construct a data Structure
• Only one – the list

(to be or not to be) has 6 atoms

• cons – can put atoms to atoms together to make a list
• car – extract atoms from a list
Primitive Functions
• car – returns the first S-expr in a list

car (A)  A

car (A B)  A

car ((A B) (C D))  (A B)

car ATOM  undefined

• cdr – returns a list containing everything but the first S-expr in a list

cdr (A)  NIL

cdr (A B)  (B)

cdr ((A B) (C D))  ((C D))

cdr ATOM  undefined

Composition of Functions

L = ( (A B) (C) (D E F))

car (car (cdr (L)))  car(car( (C) (D E F)))

 car ( C)

 C

Another Primitive Functions
• cons – construct (or concatenate)

cons (A; (B C) )  (A B C)

cons ( (A); (B C) )  ( (A) B C)

• construct s-expr z, such that

car (z) = x and cdr (z) = y

User-defined functions
• Everything is a function in LISP and program functions are lists
• Use defun
• Call function “defun” with 3 arguments
• Function name, formal parameter list, body

(defun double (N) (double 5)  10

(* N 2) )

(make-table text nil)

Q: Is this 3 atoms or a function call with two parameters?

A: function call assuming defined function make-table

If you want it to be treated as 3 atoms use a single-quote ‘(make-table text nil)

mapcar
• Code iterative functions
• mapcar takes 2 arguments
• Function name
• A list
• Returns a list
• Applies a function to each item in a list and returns resulting values in a new list
(defun double-list (lis)

(mapcar (function double) lis))

or

(defun double-list (lis)

(mapcar ‘double lis)

--the function name is an argument to mapcar function

mapcar…
• if lis = (5 2 7 4) before

(double-list lis) or (double ‘(5 2 7 4))

• then lis = (10 4 14 8) after
lambda expression
• Serve the same purpose as a “nested function”
• Anonymous functions
• Define a function within a function
• But function has no name
Do not evaluate within mapcar, DEFINE

(defun double-list (lis)

(mapcar ‘(lambda (N) (* N 2 ) ) lis) )

• Almost identical to “helping function” or subfunction
• defun and function name replaced by lambda, which has a parameter list and function body
• However appears in middle of another function;
• No way to call lambda function from any function except from one in which it is embeded
More examples

(lambda (x) (+ x 5))

(lambda (x y) (+ (* 2 x ) (* 3 y)))

(lambda (x y z) (cond (eq x NIL) y)

(T z) ) )

• Variables x, y and z are bound variables – local to lambda expression
May have unbound variables occurring in lambda expressions

(lambda (x) (+ x z))

• Here z is an unbound variable

Q: Where does it get its value when lambda expression is evaluated?

A: outer function that lambda is nested inside

(defun MultList (z lis)

(mapcar #’(lambda (x) (+ x z) ) lis ) )

Treat variables like z inside lambda

expression as local variables from

definition

Other Statements

(read) returns whatever typed in (be it a list or an atom)

(setq list (read)) I’ll type in (A B C D)

or

(setq Item (read)) I’ll type in A

Local variables in a function?
• Use let

(defun fnName (parameters)

(let ( (list nil) … (n 1) ( ) )

Set members
• Member of a set

(setq oddset ‘(1 3 5 7 9) )

(member 5 oddset)  returns T or F

Append and Reverse

(append list-A list-B)  returns a list

(reverse (concatenate ‘string “ABC” “DEF”)

Setup and Running