Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1 CS250: Intro to AI/Lisp
Projects What’s expected Project ideas Lecture 2-1 CS250: Intro to AI/Lisp
“Get Your Red-Hot Lists Here!” Conses are pairs of pointers First pointer is the car Rest is the cdr Lists are conses in which: First pointer is the first element Second pointer is the rest of the list No intermediate pointers makes last expensive USER(104): (last (list 'a 'b 'c)) (C) Lecture 2-1 CS250: Intro to AI/Lisp
Box & Pointer a c b d Represent a cons graphically (list ‘a (list ‘b ‘c) ‘d) a c b d nil Lecture 2-1 CS250: Intro to AI/Lisp
eq True if its arguments are the same, identical object; otherwise, returns false (eq 'a 'b) => false (eq 'a 'a) => true (eq 3 3) => true OR => false (eq 3 3.0) => false Lecture 2-1 CS250: Intro to AI/Lisp
More on eq Crazy things can happen Question from last time: Implementations can “collapse” constants (eq “Foo” “Foo”) could be TRUE or FALSE If interpreted, it’s always FALSE Question from last time: (eq ‘(a .b) ‘(a . b)) could be TRUE or FALSE (eq (cons ‘a ‘b) (cons ‘a ‘b)) is FALSE Lecture 2-1 CS250: Intro to AI/Lisp
eql True of two objects, x and y, in the folowing cases: 1. If x and y are eq. 2. If x and y are both numbers of the same type and the same value. 3. If they are both characters that represent the same character. (eql 'a 'b) => false (eql 'a 'a) => true (eql 3 3) => true (eql 3 3.0) => false (eql 3.0 3.0) => true (eql #c(3 -4) #c(3 -4)) => true (eql #c(3 -4.0) #c(3 -4)) => false Lecture 2-1 CS250: Intro to AI/Lisp
eq vs. eql eql tells whether two objects are conceptually the same eq tells whether two objects are implementationally the same Lecture 2-1 CS250: Intro to AI/Lisp
= Numeric comparisons USER(3): (= 3 3 3 3) T USER(4): (= 3 3.0 3 3 3) NIL Lecture 2-1 CS250: Intro to AI/Lisp
Sequences Sequences are ordered “sets” Many useful functions: Lists + vectors Many useful functions: elt reverse map remove length nreverse some delete ... Lecture 2-1 CS250: Intro to AI/Lisp
Association lists Easy lookup tables Try this (setf nickname '(("Red Sox" . "Losers") ("Mets" . "Wannabes") ("Yankees" . "World champions"))) What is returned by: (assoc “Red Sox” nickname) Lecture 2-1 CS250: Intro to AI/Lisp
Keyword args Keywords enable flexibility in arguments Playing with args: &rest &optional &key Lecture 2-1 CS250: Intro to AI/Lisp
The rest of the story How does #’+ work? How to handle an unknown number: Use a list Use rest Testing for nil (defun any-nil-p (&rest all-values) (notevery #'null (mapcar #'null all-values))) Lecture 2-1 CS250: Intro to AI/Lisp
Optional parameters Add an optional parameter with a default value ((lambda (a &optional (b 2)) (+ a (* b 3))) 4 5) ((lambda (a &optional (b 2)) (+ a (* b 3))) 4) Lecture 2-1 CS250: Intro to AI/Lisp