Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,

Similar presentations


Presentation on theme: "Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,"— Presentation transcript:

1 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&NR=1

2 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”

3 Function examples plustwo(x) ::= plusone (plusone(x)) where plusone(x) ::= x+1 fact(n) ::= if n = 0 then 1 else n * fact(n-1)

4 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)

5 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)

6 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 )

7 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;

8 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

9 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

10 Composition of Functions L = ( (AB) (C) (D E F)) car (car (cdr (L)))  car(car( (C) (D E F)))  car ( C)  C

11 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

12 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) )

13 (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)

14 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

15 (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

16 mapcar… if lis = (5 2 7 4) before (double-list lis) or (double ‘ (5 2 7 4)) then lis = (10 4 14 8) after

17 lambda expression Serve the same purpose as a “nested function” Anonymous functions –Define a function within a function –But function has no name

18 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

19 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

20 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

21 (defun MultList (z lis) (mapcar # ’ (lambda (x) (+ x z) ) lis ) ) Treat variables like z inside lambda expression as local variables from definition

22 Other Statements Reading (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

23 Local variables in a function? Use let (defun fnName (parameters) (let ( (list nil) … (n 1) ( ) )

24 Set members Member of a set (setq oddset ‘(1 3 5 7 9) ) (member 5 oddset)  returns T or F

25 Append and Reverse (append list-A list-B)  returns a list (reverse (concatenate ‘ string “ABC” “DEF”)

26 Setup and Running Download http://sourceforge.net/projects/clisp/ http://sourceforge.net/projects/clisp/ CLISP - Brief introduction to install and setup of an artificially intelligent environment (YouTube video)CLISP - Brief introduction to install and setup of an artificially intelligent environment Demo


Download ppt "Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,"

Similar presentations


Ads by Google