From: Ramakrishnan Muthukrishnan Date: Sun, 2 Jan 2011 17:05:35 +0000 (+0530) Subject: solutions to ex 3.1, 3.2, 3.3 and 3.4 X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/file/URI:LIT:krugkidfnzsc4/@@named=/statistics?a=commitdiff_plain;h=a09d9be5915e8745aebbeb3973e97a9db706db73;p=sicp.git solutions to ex 3.1, 3.2, 3.3 and 3.4 --- diff --git a/src/sicp/ex3_1.rkt b/src/sicp/ex3_1.rkt new file mode 100644 index 0000000..4bd1ef9 --- /dev/null +++ b/src/sicp/ex3_1.rkt @@ -0,0 +1,7 @@ +#lang racket + +(define (make-accumulator acc) + (lambda (val) + (begin + (set! acc (+ acc val)) + acc))) \ No newline at end of file diff --git a/src/sicp/ex3_2.rkt b/src/sicp/ex3_2.rkt new file mode 100644 index 0000000..de275ef --- /dev/null +++ b/src/sicp/ex3_2.rkt @@ -0,0 +1,29 @@ +#lang racket + +(define (make-monitored f) + (let ((num-calls 0)) + (lambda (x) + (cond + ((eq? x 'how-many-calls?) num-calls) + (else + (begin + (set! num-calls (+ num-calls 1)) + (f x))))))) + +#| +> (define (square x) (* x x)) +> (square 4) +16 +> (define mf (make-monitored square)) +> (mf 'how-many-calls?) +0 +> (mf 10) +100 +> (mf 'how-many-calls?) +1 +> (mf 100) +10000 +> (mf 'how-many-calls?) +2 +> +|# \ No newline at end of file diff --git a/src/sicp/ex3_3.rkt b/src/sicp/ex3_3.rkt new file mode 100644 index 0000000..0f6b79e --- /dev/null +++ b/src/sicp/ex3_3.rkt @@ -0,0 +1,19 @@ +#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) \ No newline at end of file diff --git a/src/sicp/ex3_4.rkt b/src/sicp/ex3_4.rkt new file mode 100644 index 0000000..ac8e334 --- /dev/null +++ b/src/sicp/ex3_4.rkt @@ -0,0 +1,48 @@ +#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) + (let ((bad-password-count 0)) + (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) + (begin + (set! bad-password-count (+ bad-password-count 1)) + (if (>= bad-password-count 7) + "Something is wrong. Calling 911..." + "Incorrect password"))))) + dispatch)) + +#| + +> (define acc (make-account 100 'secret-password)) +> ((acc 'secret-password 'withdraw) 40) +60 +> ((acc 'some-other-password 'deposit) 50) +"Incorrect password" +> ((acc 'some-other-password 'deposit) 50) +"Incorrect password" +> ((acc 'some-other-password 'deposit) 50) +"Incorrect password" +> ((acc 'some-other-password 'deposit) 50) +"Incorrect password" +> ((acc 'some-other-password 'deposit) 50) +"Incorrect password" +> ((acc 'some-other-password 'deposit) 50) +"Incorrect password" +> ((acc 'some-other-password 'deposit) 50) +"Something is wrong. Calling 911..." +> + +|# \ No newline at end of file