From: Ramakrishnan Muthukrishnan Date: Sat, 8 Jan 2011 10:33:17 +0000 (+0530) Subject: solutions to 3.7 and 3.8. Very thought provoking indeed... X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/biz/?a=commitdiff_plain;h=c9f43faf3eef9510b5afbf19618e34ca75f03f78;p=sicp.git solutions to 3.7 and 3.8. Very thought provoking indeed... --- diff --git a/src/sicp/ex3_7.rkt b/src/sicp/ex3_7.rkt new file mode 100644 index 0000000..51d01e2 --- /dev/null +++ b/src/sicp/ex3_7.rkt @@ -0,0 +1,27 @@ +#lang racket + +(define (make-account balance password) + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) + balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (define (dispatch given-password m) + (if (eq? given-password password) + (cond ((eq? m 'withdraw) (lambda (amt) (withdraw amt))) + ((eq? m 'deposit) (lambda (amt) (deposit amt))) + (else (error "Unknown request -- MAKE-ACCOUNT" + m))) + (lambda (temp) "Incorrect password"))) + dispatch) + +(define (make-joint account acc-password joint-password) + (define (dispatch given-password m) + (if (eq? given-password joint-password) + (account acc-password m) + (lambda (temp) "incorrect password!"))) + dispatch) + \ No newline at end of file diff --git a/src/sicp/ex3_8.rkt b/src/sicp/ex3_8.rkt new file mode 100644 index 0000000..6274bad --- /dev/null +++ b/src/sicp/ex3_8.rkt @@ -0,0 +1,12 @@ +#lang racket + +(define f + (let ((x 0)) + (lambda (y) + (set! x (+ x 1)) + (cond + ((and (= x 1) (= y 0)) -1) + ((and (= x 2) (= y 1)) 1) + ((and (= x 1) (= y 1)) 0) + ((and (= x 2) (= y 0)) 1))))) + \ No newline at end of file