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.
(deriv-2 '((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 make-exponentiation 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
(same-variable? exp var) 1 0))
((sum? exp) (make-sum (deriv
(addend exp) var) (deriv (augend exp) var)))
((product? exp) (make-sum
(make-product (multiplier exp) (deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var) (multiplicand exp))))
(else
(error "unknown
expression type -- DERIV" exp))))
(define variable? symbol?)
(define (same-variable? 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 make-product has been corrected
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else (list '+ a1 a2))))
(define (make-product 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 (make-table)
(let ((local-table (list '*table*)))
(define (lookup key-1 key-2)
(let ((subtable (assoc key-1 (cdr
local-table))))
(if subtable
(let
((record (assoc key-2 (cdr subtable))))
(if record
(cdr record)
false))
false)))
(define (insert! key-1 key-2 value)
(let ((subtable (assoc key-1 (cdr
local-table))))
(if subtable
(let
((record (assoc key-2 (cdr subtable))))
(if record
(set-cdr! record value)
(set-cdr! subtable
(cons (cons key-2 value)
(cdr subtable)))))
(set-cdr! local-table
(cons (list key-1
(cons key-2 value))
(cdr local-table)))))
'ok)
(define (dispatch m)
(cond ((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc!) insert!)
(else (error "Unknown operation -- TABLE" m))))
dispatch))
(define operation-table (make-table))
(define get (operation-table 'lookup-proc))
(define put (operation-table 'insert-proc!))
(define (deriv-2 exp var)
(cond ((number? exp) 0)
((variable? exp) (if
(same-variable? 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
make-exponentiation here
;;exercise 2.75
(define (apply-generic 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 |