Reinforcement Learning and
Artificial
Intelligence (RLAI) 

CMPUT 325: Assignment 4

Due: Monday, 16 October, 2006 by 23:59:59
Specs:
Do exercises 2.53, 2.54, 2.55, 2.58a, 2.62, 2.73 and 2.75
from Structure and Interpretation of
Computer Programs (second edition).
Please follow the rules given below. If there is a discrepancy
between the text and the rules below, follow our instructions.
Submission Instructions:
Write all your answers in a plain text file named a4.
From
the directory where this file is located, type in the following command:
astep c c325 p ex4 a4When prompted if this is your primary submission, answer Y. You may submit as many times as you like; the last version you submit will be marked. All submissions should be primary submissions, not just the last.
(deriv2 '((3 * x) ** 2) 'x)
should return ((2
* (3 * x)) * 3)
. There will be more examples in the
provided test cases. In
addition to adding another put
call into your file, you must also define the constructor makeexponentiation as specified in
exercise 2.56. You need not define any other procedures, but may
if you find them helpful
;;exercise 2.58a
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if
(samevariable? exp var) 1 0))
((sum? exp) (makesum (deriv
(addend exp) var) (deriv (augend exp) var)))
((product? exp) (makesum
(makeproduct (multiplier exp) (deriv (multiplicand exp) var))
(makeproduct (deriv (multiplier exp) var) (multiplicand exp))))
(else
(error "unknown
expression type  DERIV" exp))))
(define variable? symbol?)
(define (samevariable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2)))
(define (=number? exp num)
(and (number? exp) (= exp num)))
;;;;alter this code
;;;;note that the definition for makeproduct has been corrected
(define (makesum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else (list '+ a1 a2))))
(define (makeproduct a1 a2)
(cond ((or (=number? a1 0)
(=number? a2 0)) 0)
((=number? a1 1) a2)
((=number? a2 1) a1)
((and (number? a1) (number? a2)) (* a1 a2))
(else (list '* a1 a2))))
(define (sum? x)
(and (pair? x) (eq? (car x)
'+)))
(define (addend s) (cadr s))
(define (augend s) (caddr s))
(define (product? x)
(and (pair? x) (eq? (car x)
'*)))
(define (multiplier s) (cadr s))
(define (multiplicand s) (caddr s))
;;exercise 2.73
(define (maketable)
(let ((localtable (list '*table*)))
(define (lookup key1 key2)
(let ((subtable (assoc key1 (cdr
localtable))))
(if subtable
(let
((record (assoc key2 (cdr subtable))))
(if record
(cdr record)
false))
false)))
(define (insert! key1 key2 value)
(let ((subtable (assoc key1 (cdr
localtable))))
(if subtable
(let
((record (assoc key2 (cdr subtable))))
(if record
(setcdr! record value)
(setcdr! subtable
(cons (cons key2 value)
(cdr subtable)))))
(setcdr! localtable
(cons (list key1
(cons key2 value))
(cdr localtable)))))
'ok)
(define (dispatch m)
(cond ((eq? m 'lookupproc) lookup)
((eq? m 'insertproc!) insert!)
(else (error "Unknown operation  TABLE" m))))
dispatch))
(define operationtable (maketable))
(define get (operationtable 'lookupproc))
(define put (operationtable 'insertproc!))
(define (deriv2 exp var)
(cond ((number? exp) 0)
((variable? exp) (if
(samevariable? exp var) 1 0))
(else ((get 'deriv (operator
exp)) (operands exp) var))))
(define operator cadr)
(define (operands x) (list (car x) (caddr x)))
;;;add three calls to put here
;;;add definition of
makeexponentiation here
;;exercise 2.75
(define (applygeneric op arg) (arg op))
Question 
Marks 
2.53 
1 
2.54 
2 
2.55 
1 
2.58a 
3 
2.62 
3 
2.73 
8 
2.75 
3 
Total 
21 
Cumulative
Total 
99 