slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lisp PowerPoint Presentation
Download Presentation
Lisp

Loading in 2 Seconds...

play fullscreen
1 / 26

Lisp - PowerPoint PPT Presentation


  • 256 Views
  • Uploaded on

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. Pure Functions. Pure functions avoid “side effects”

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

PowerPoint Slideshow about 'Lisp' - jacob


An Image/Link below is provided (as is) to download presentation

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

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

slide13
(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
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
slide15
(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

mapcar1
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
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
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
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
slide20
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

slide21
(defun MultList (z lis)

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

Treat variables like z inside lambda

expression as local variables from

definition

other statements
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

local variables in a function
Local variables in a function?
  • Use let

(defun fnName (parameters)

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

set members
Set members
  • Member of a set

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

(member 5 oddset)  returns T or F

append and reverse
Append and Reverse

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

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

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