Programming in Your Favorite Language - PowerPoint PPT Presentation

About This Presentation
Title:

Programming in Your Favorite Language

Description:

domain (iota n))) (iota n)) :assigned nil :constraint-fn (if explicit? ... domain (iota n))) (iota n)) USER(105): (iota 8) (0 1 2 3 4 5 6 7) Lecture 6-2 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 25
Provided by: kurtdfenst
Category:

less

Transcript and Presenter's Notes

Title: Programming in Your Favorite Language


1
Programming in Your Favorite Language
  • Lecture 5-2
  • February 11th, 1999
  • CS250

2
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)
3
Box Pointer
  • Represent a cons graphically

(list a (list b c) d)
4
Some Things are More Equal than Others
  • Lisp has multiple definitions of equality
  • Decreasing order of strictness
  • eq, eql, equal

5
eq
  • True if its arguments are the same, identical
    object otherwise, returns false

(eq 'a 'b) gt false (eq 'a 'a) gt true (eq 3
3) gt true OR gt false (eq 3 3.0) gt false
6
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) gt false (eql 'a 'a) gt true
(eql 3 3) gt true (eql 3 3.0) gt false (eql
3.0 3.0) gt true (eql c(3 -4) c(3 -4)) gt
true (eql c(3 -4.0) c(3 -4)) gt false
7
equal
  • Generally, returns true if two objects print the
    same

gt (setf x (cons a nil)) (A) gt (eql x x) T gt
(equal x (cons a nil)) T
8
Mapping over lists
  • Need to do something to every element in a list?
    Try a mapping function
  • mapcar for using the car of successive cdrs
  • maplist for successive cdrs themselves

9
mapcar in Action
USER(115) (mapcar 'list '(a b c) '(1 2 3 4))
((A 1) (B 2) (C 3))
USER(116) (mapcar 'list '(a b c) '(1 2))
((A 1) (B 2))
10
Creating an N-Queens Problem
(defun nqueens-initial-state (n optional
(explicit? nil) (complete? nil)) (let ((s
(make-CSP-state unassigned (mapcar
'(lambda (var) (make-CSP-var name
var domain (iota n))) (iota n))
assigned nil constraint-fn (if
explicit? (let ((constraints
(nqueens-constraints n))) '(lambda (var1
val1 var2 val2) (CSP-explicit-check
var1 val1 var2 val2 constraints)))
'nqueens-constraint-fn)))) (if complete?
(CSP-random-completion s) s)))
11
Unassigned Variables
(mapcar '(lambda (var) (make-CSP-var name
var domain (iota n))) (iota n))
USER(105) (iota 8) (0 1 2 3 4 5 6 7)
12
Unassigned Variables II
((0 (0 1 2 3 4 5 6 7) NIL NIL) (1 (0 1 2 3 4 5 6
7) NIL NIL) (2 (0 1 2 3 4 5 6 7) NIL NIL) (3 (0
1 2 3 4 5 6 7) NIL NIL) (4 (0 1 2 3 4 5 6 7) NIL
NIL) (5 (0 1 2 3 4 5 6 7) NIL NIL) (6 (0 1 2 3
4 5 6 7) NIL NIL) (7 (0 1 2 3 4 5 6 7) NIL NIL))
13
Recursion Again
  • Recursive function Base case Recursive step
  • Base case will be a conditional test, plus a call
    that returns
  • Example General-Search

(defun general-search-helper (problem nodes
queuing-fn) (let ((node (first nodes))) (if
(null node) nil
14
Recursive General Search
If weve got a node, what do we do next?
(if (goal-test problem (node-state node)) node
What if its not the goal?
(general-search-helper problem (funcall
queuing-fn (rest nodes)
(expand node problem))
queuing-fn)...)
15
Put it Together
(defun general-search-helper (problem nodes
queuing-fn) (let ((node (first nodes))) (if
(null node) nil (if (goal-test
problem (node-state node)) node
(general-search-helper problem (funcall
queuing-fn (rest nodes) (expand node
problem)) queuing-fn)))))
16
Getting it Started...
From simple.lisp
General-Search function
(let ((nodes (make-initial-queue problem
queuing-fn)) node)
How does Make-Initial-Queue work?
(defun make-initial-queue (problem queuing-fn)
(let ((q (make-empty-queue))) (funcall
queuing-fn q (list (create-start-node
problem))) q))
17
Top-level Function
(defun general-search-recursive (problem
queueing-fn) "Recursive version of general
search" (general-search-helper problem
(list (create-start-node problem))
queueing-fn))
18
BFS with a List
Whats the rule for node exploration in BFS? How
are new nodes added?
(defun breadth-first-search (problem)
(general-search-recursive problem 'append))
19
Sets
  • Sets let you treat lists as sets
  • Membership
  • Union, intersection
  • Set difference

20
Sequences
  • Sequences include more than just lists
  • Ordered series
  • Lists and vectors
  • Many functions operate on sequences, not just
    lists
  • length, sort, subseq, reverse, every, some, elt

21
Structures
  • Create records in Lisp
  • Define structures with the defstruct macro

(defstruct point x y)
22
Big Sale on Constructors Accessors!
  • Creating a structure creates
  • Constructor (make-point)
  • Arguments are passed by keyword
  • Copy constructor (copy-point)
  • Slot accessor functions (point-x, point-y)
  • Type predicate (point-p)
  • New structures are new types

23
Default Values for Structure Fields
  • Add a default value

(defstruct midterm (difficulty (progn
(format t How hard was it?) (read)))
(max-grade 54) (num-completed nil))
24
Customize Automatic Functions
(defstruct (point (conc-name p)
(print-function print-point)) (x 0) (y
0)) (defun print-point (p stream depth)
(format stream ltA, Agt (px p) (py p)))
Write a Comment
User Comments (0)
About PowerShow.com