Introduction to LISP - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to LISP

Description:

Introduction to LISP Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures You can write programs that write ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 28
Provided by: laura
Category:

less

Transcript and Presenter's Notes

Title: Introduction to LISP


1
Introduction to LISP
2
Lisp
  • Extensible It lets you define new operators
    yourself
  • Lisp programs are expressed as lisp data
    structures
  • You can write programs that write programs
    (macros)

3
Prefix notation
  • gt( 2 3)
  • 5
  • gt( 2 3 4 5)
  • 14
  • gt(/ (- 7 1) (- 4 2))
  • 3

4
Prefix notation evaluation
  • gt(/ (- 7 1) (- 4 2))
  • 3
  • Arguments from left to right
  • Arguments are passed to the function

5
Data
  • Integer
  • gt256
  • 256
  • Strings
  • gt"hello world
  • "hello world
  • Symbols
  • Symbols are words
  • gthello
  • HELLO
  • Usually converted to uppercases
  • Lisp is case insensitive
  • Lists

6
Quote
  • gt(quote ( 3 5))
  • ( 3 5)
  • gt( ( 3 5))
  • ( 3 5)
  • quote is a special operator (distinct evaluation
    rule)
  • Protects expressions from evaluation

7
Lists
  • Lists are zero or more elements enclosed in
    parentheses
  • gt(list 3 2 1)
  • (3 2 1)
  • gt(3 2 1)
  • (3 2 1)
  • gt(list '( 2 1) ( 2 1))
  • (( 2 1) 3)

8
Lists
  • Lisp programs are expressed as lists
  • This is why we need the quote
  • Quoted list returns the list itself
  • Unquoted list trated as code and returns its
    evaluation

9
List operations
  • gt(cons a (b c d))
  • (A B C D)
  • gt(car (a b c d))
  • A
  • gt(cdr (a b c d))
  • (B C D)
  • gt(car (cdr (a b c d)))
  • B
  • second, third, nth

10
Truth
  • T, NIL, null, not
  • gt(listp (a b c))
  • T
  • (gtlistp 27)
  • NIL
  • gt(null nil)
  • T
  • gt(not nil)
  • T
  • numberp, listp, stringp,

11
Conditionals
  • (if (listp (a b c))
  • ( 1 2)
  • ( 5 6))
  • 3
  • (if (listp 27)
  • ( 2 3))
  • NIL

12
AND / OR
  • (and t NIL)
  • NIL
  • (and t ( 1 3))
  • 3
  • (or t NIL)
  • T
  • (or t ( 1 3))
  • T

13
Functions
  • (defun our-third (x)
  • (car (cdr (cdr x)))
  • gt(our-third (A B C D))
  • C

14
Recursion
  • (defun our-member (obj lst)
  • (if (null lst)
  • nil
  • (if (eql (car lst) obj)
  • T
  • (our-member obj (cdr lst)))))

15
Iteration
  • (defun show-squares (start end)
  • (do ((i start ( i 1 )))
  • ((gt I end) done)
  • (format t s s i ( i i))))
  • We do not always need recursion.
  • When we want to do something repeatedly,
    iteration is sometimes more natural
  • A typical case is to generate some sort of table
    or iterating over an array
  • Prints out the squares of the integers from start
    to end

16
Recursive show-squares
  • For comparison, here is a recursive version of
    show-squares.
  • (defun show-squares (i end)
  • (if (gt i end) done
  • (progn
  • (format t s s i ( i i))
  • (show-squares ( i 1) end))))
  • Note progn is an special operator that evaluates
    its arguments in order and returns the value(s)
    of the last.

17
Functional programming
  • gt(setf lst (c a r a t))
  • (C A R A T)
  • (remove a lst)
  • (C R T)
  • Removes does not remove an object from a list
    (not literally)
  • The function returns a new list containing
    everything on the original list but the object
  • Use (setf x (remove a x)) instead

18
Functions as objects
  • In Lisp, functions are regular objects (like
    symbols or strings or lists)
  • Like any other kind of object, we can pass
    functions as arguments. One example of a function
    that takes a function as parameter is apply
  • gt(apply (1 2 3))
  • 6
  • (sharp-quote) is an abbreviation for function
    just as is an abbreviation for quote
  • gt(apply (function ) (1 2 3))
  • 6
  • The function funcall does the same thing but does
    not need the arguments to be packaged in a list
  • gt(funcall 1 2 3)
  • 6

19
Equality
  • gt (eql '(1 2) '(1 2))
  • NIL
  • gt (equal '(1 2) '(1 2))
  • T
  • gt (eql 1 1)
  • T
  • gt (equal 1 1)
  • T
  • gt ( 1 1)
  • T
  • gt ( 1 1.0)
  • T
  • gt (eql 1 1.0)
  • NIL
  • gt (equal 1 1.0)
  • NIL

20
Mapping functions
  • gt (mapcar 'sqrt '(4 9 16))
  • (2 3 4)
  • Mapcar takes a function and a list and returns
    the result of applying the function to every
    element from the list.
  • Other map functions
  • maplist, mapcan, mapcon,

21
Some functions you might need
  • car
  • cdr
  • defun
  • if
  • lambda
  • list
  • listp
  • stringp
  • loop
  • mapcar
  • not
  • defvar
  • setf
  • setq
  • numberp
  • funcall
  • remove
  • defmacro

22
Some useful functions for the homework
  • Instantiate the game class
  • (setq mypuzzle (make-instance 'game board
    easy-test name easy))
  • Creates an instance of the class game and sets
    the att board to easy-test and the att name to
    easy
  • To access the atts (above we just set them when
    creating the instance), use the accessors defined
    in the defclass
  • (game-board mypuzzle)
  • (game-name mypuzzle)

23
Some useful functions for the homework
  • Use the functions provided. Particularly
    row-groups, column-groups, and block-groups
  • (setq rows (ROW-GROUPS (game-board mypuzzle)))
  • (setq cols (COLUMN-GROUPS (game-board mypuzzle)))
  • (setq blocks (BLOCK-GROUPS (game-board
    mypuzzle)))
  • game-board is the accessor to the board attribute
    in the game class
  • You can print the lists
  • gt(print rows)
  • ((2 4 3 1) (1 3 4 2) (4 2 1 3) (3 1 2 -))

24
Some useful functions for the homework
  • (not (member t (mapcar 'duplicate-values rows)))
  • no duplicate values on the rows of the board
    (rows is a list of lists as you can see on
    previous slide)
  • (defun duplicate-values (lst)
  • (has-duplicates (sort (remove '- lst)
    'lt)))
  • has-duplicates should be a recursive function
    similar to our-member defined on previous slides.
    The logic is
  • return NIL for lists of size 1 or less
  • return true if the first and second element are
    equal
  • call has-duplicates on the rest of the list

25
Some useful functions for the homework
  • Removes the dashes from the list lst
  • (remove - lst)
  • Member which returns NIL if the list does not
    contain the element
  • gt (member 1 '(2 3 4))
  • NIL
  • (not (member t (mapcar 'duplicate-values rows)))
  • no duplicate values on the rows of the board
    (rows is a list of lists as you can see on
    previous slide)

26
Some useful functions for the homework
  • iota creates a list of numbers within a range
  • gt (iota 10 1)
  • (1 2 3 4 5 6 7 8 9 10)
  • sort returns a the list sorted according to a
    comparison function
  • gt (sort '(5 3 4 1 9) 'lt)
  • (1 3 4 5 9)
  • To check if a list (e.g. a row, a column, a
    block) has all the values from 1 to MAXX (useful
    function for defining goalp)
  • (defun all-values (lst)
  • (equal (iota MAXX 1) (sort (remove '-
    lst) 'lt) ))

27
From the code from the book
  • http//aima.cs.berkeley.edu/
  • Use utilities.lisp if you want to use the iota
    function
  • You might want to use queue.lisp too (includes
    functions for queues maniulations)
  • You might want to check simple.lisp for the
    general search (an idea of how to implement it)
Write a Comment
User Comments (0)
About PowerShow.com