Docsity

Lisp Programming - Embedded Intelligent Robotics - Lecture Slides, Slides of Robotics

Partial preview of the text

Download Lisp Programming - Embedded Intelligent Robotics - Lecture Slides and more Slides Robotics in PDF only on Docsity!

Introductory Lisp Programming Lecture # 2

  • Main Topics
    • Basic Lisp data types
    • Lisp primitives
    • Details of list handling
      • Cons cells (boxes) & their representation in memory
      • List operators: cons, append, rplaca, rplacd..
      • Lists as arguments to functions

Lisp Lecture #

  • Exam #
    • Will cover Lisp programming
    • February ???-th, take-home
  • Today:
    • Review lecture on Lisp
    • More on functions
    • More on Lisp internals
    • Non-destructive and destructive operations
  • Next time:
    • Problem solving using search (Chapter 3 R&N)
    • Read chapter before coming to class

Logical Operators

Truth values = T (true) and NIL (false)

AND, OR, NOT, XOR..

Everything but NIL has a truth value of T - even 0!!! (and t t) => t; (and 1 0) => t ; (or nil nil 0) => t; (xor 1 t) => nil (not t) =>nil; (not nil) => t; (not 45) => nil;

*Logical operators only evaluate as many arguments (left to right) as necessary to determine the truth value of the expression.

(and nil (* 88 99 66)) => nil ;; the * is never evaluated

(and (* 88 99) nil) => nil ;; the * IS evaluated due to L-R eval order

(not (and (or nil nil) (xor 1 0))) => t ;; the xor is never evaluated

  • NIL is both false and the empty list, depending upon the context

a (^) b

Dotted Pairs

(cons โ€˜a nil) => (a) (cons โ€˜a (cons โ€˜b nil)) => (a b) (cons โ€˜a โ€˜(b)) => (a b) (cons โ€˜a โ€˜b) => (a. b) (cons โ€˜(a b) โ€˜c) => ((a b). c) (cons (cons โ€˜a โ€˜b) โ€˜c) => ((a. b). c)

a a b

a b

c

a (^) b

c

(car โ€˜(a. b)) => a (cdr โ€˜(a. b)) => b ; symbol (cdr โ€˜(a b)) => (b) ; cons cell

  • Optional

arguments

Binds to the car of

list of arguments

Binds to the cdr of

list of arguments

Optional argument

(root 4)==>

(root 4 3)==>4 1/

More on Lisp Internals

  • Storage allocation
  • Non-destructive list operations
  • Destructive list operations
  • Pointer manipulation: setf, eq
  • Efficiency considerations
  • equal
  • eq

Two pointers point to the

same location

  • Garbage

collection

Efficiency Considerations

res is optional argument Argument list

Variable Assignment & Access

Assignment

SETF

(setf y (+ 3 5)) => y assigned the value 8 (setf y โ€˜((a 1) (b 2) (c 3))) => y assigned to the data list

  • Can be used to set arbitrary locations in Lisp data structures

(setf (second y) 88) => 88 y => ((a 1) 88 (c 3))

SETQ

(setf x (+ 3 5)) => x assigned the value 8

  • First argument must be a symbol, not a general location.

Access

Anytime a variable appears, it will be evaluated (i.e. its value will be accessed), unless the variable appears inside the scope of a single or double quote, or is an argument to a macro.

x => 8

โ€˜x => x

Evaluating LISP Expressions (2)

A single quoted list evaluates to a simple list of symbols

โ€˜(a b c) => (a b c)

โ€˜(+ 2 3) => (+ 2 3)

An unquoted list evaluates to a function call

(+ 2 3) => 5

(a b c) => ERROR: attempt to call an undeclared function โ€˜a

Evaluate arguments left-to-right, then call function:

(setf bob 5 bill 7 sue 9)

(+ bob bill sue) => 21 ;; (Calls + with 3 values: 5, 7, 9)

Polish notation: (operator operand1 operand2 โ€ฆ operandn)

(+ 2 3 4 5) simpler than 2 + 3 + 4 + 5 + 6 if you can live with all the parentheses!!

Parentheses => no need for arithmetic precedence rules

(+ (* a b) (* c d)) -vs- ab + cd Docsity.com

Depth-First Evaluation Order

  • (f1 (f2 a (f3 b)) c 8 (f2 3 2))

f

f

a f

b

c 8 f

List Constructors

CONS (โ€œconstructโ€)

(cons โ€˜a โ€˜(a b c)) => (a a b c); (cons โ€˜(a b) โ€˜(c d)) => โ€˜((a b) c d) (cons โ€˜a nil) => (a); (cons nil โ€˜(a b)) => (nil a b)) = (() a b)

LIST

(list โ€˜a โ€˜b) => (a b); (list โ€˜(a b) โ€˜c โ€˜d) => ((a b) c d)

APPEND

(append โ€˜(a b c) โ€˜(d e f)) => (a b c d e f); (append nil โ€˜(a b)) => (a b)

These are not destructive :

(setf x โ€˜(a b c)) (cons 62 x) => (62 a b c) x => (a b c)

  • Execution of

cons