Lisp Functional Language or Applicative Language –Achieves its effect by applying functions,...

26
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

Transcript of Lisp Functional Language or Applicative Language –Achieves its effect by applying functions,...

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

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

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

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”

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

Function examples

plustwo(x) ::= plusone (plusone(x))where plusone(x) ::= x+1

fact(n) ::= if n = 0 then 1 else n * fact(n-1)

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

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)

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

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)

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

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 )

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

Conditions

(cond

( (null x) 0)

( (eq x y) (f x) )

( T (g y) ) )

if null (x) then 0elseif x == y then f(x)else g(y)endif;

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

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

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

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

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

Composition of Functions

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

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

car ( C)

C

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

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

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

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

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

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

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

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

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

(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

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

mapcar…

• if lis = (5 2 7 4) before (double-list lis) or (double ‘(5 2 7 4))

• then lis = (10 4 14 8) after

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

lambda expression

• Serve the same purpose as a “nested function”

• Anonymous functions– Define a function within a function– But function has no name

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

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

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

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

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

• 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

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

(defun MultList (z lis)

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

Treat variables like z inside lambdaexpression as local variables from definition

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

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

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

Local variables in a function?

• Use let (defun fnName (parameters)

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

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

Set members

• Member of a set

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

(member 5 oddset) returns T or F

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

Append and Reverse

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

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

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

Setup and Running

• Download http://sourceforge.net/projects/clisp/

• CLISP - Brief introduction to install and setup of an artificially intelligent environment (YouTube video)

• Demo