140 likes | 273 Views
Dive into the world of Lisp programming with this CS250 lecture summary covering topics like conses, eql, sequences, and optional parameters. Understand the nuances of lists, equality functions, and handling parameters efficiently in Lisp.
E N D
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 CS250: Intro to AI/Lisp
Projects • What’s expected • Project ideas 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) CS250: Intro to AI/Lisp
nil d a nil b c Box & Pointer • Represent a cons graphically (list ‘a (list ‘b ‘c) ‘d) 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 CS250: Intro to AI/Lisp
More on eq • Crazy things can happen • 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 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 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 CS250: Intro to AI/Lisp
= • Numeric comparisons USER(3): (= 3 3 3 3) T USER(4): (= 3 3.0 3 3 3) T USER(5): (= 2 3.0 3 3 3) NIL CS250: Intro to AI/Lisp
Sequences • Sequences are ordered “sets” • Lists + vectors • Many useful functions: elt reverse map remove length nreverse some delete ... 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) CS250: Intro to AI/Lisp
Keyword args • Keywords enable flexibility in arguments • Playing with args: • &rest • &optional • &key 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))) 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) CS250: Intro to AI/Lisp