Permalink
Browse files

first commit

  • Loading branch information...
1 parent f577bc6 commit eb2964e9d0c7584567ac9186cfe58c5fd5b66be6 @OuZhencong committed Apr 4, 2013
Showing with 131 additions and 1 deletion.
  1. +2 −1 README.md
  2. +13 −0 chapter3/exercise3-1.scm
  3. +37 −0 chapter3/exercise3-2.scm
  4. +31 −0 chapter3/exercise3-3.scm
  5. +48 −0 chapter3/exercise3-4.scm
View
@@ -1,2 +1,3 @@
sicp
-====
+====
+scip exercises
View
@@ -0,0 +1,13 @@
+#lang racket
+(define (make-accumulator balance)
+ (lambda (amount)
+ (set! balance (+ balance amount))
+ balance))
+
+;Usage:
+(define A (make-accumulator 5))
+
+(A 10)
+;15
+(A 20)
+;35
View
@@ -0,0 +1,37 @@
+#lang racket
+(define (make-monitored fun)
+ (let ((count 0))
+ (define (how-many-calls?)
+ count)
+ (define (reset-count)
+ (set! count 0))
+ (define (call-fun arg)
+ (set! count (+ count 1))
+ (fun arg))
+ (define (dispatch m)
+ (cond
+ ((eq? m 'how-many-calls?)
+ (how-many-calls?))
+ ((eq? m 'reset-count)
+ (reset-count))
+ (else
+ (call-fun m))))
+ dispatch))
+
+;Usage:
+(define s (make-monitored sqrt))
+
+(s 100)
+;10
+(s 9)
+;3
+(s 25)
+;5
+
+(s 'reset-count)
+
+(s 16)
+;4
+
+(s 'how-many-calls?)
+;1
View
@@ -0,0 +1,31 @@
+#lang racket
+(define (make-accout 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 (error-msg arg)
+ "Incorrect Password")
+ (define (dispatch input-pass m)
+ (if (eq? input-pass password)
+ (begin
+ (cond
+ ((eq? m 'withdraw) withdraw)
+ ((eq? m 'deposit) deposit)
+ (else (error "Unknown request" m))))
+ error-msg))
+ dispatch)
+
+;Usage:
+(define acc (make-accout 100 'admin))
+
+((acc 'admin 'withdraw) 50)
+;50
+((acc 'root 'deposit) 50)
+;"Incorrect Password"
+((acc 'admin 'withdraw1) 20)
+;Unknown request withdraw1
View
@@ -0,0 +1,48 @@
+#lang racket
+(define (make-accout balance password)
+ (define incorrect-count 0)
+ (define (withdraw amount)
+ (if (>= balance amount)
+ (begin (set! balance (- balance amount))
+ balance)
+ "Insufficient funds"))
+ (define (deposit amount)
+ (set! balance (+ balance amount))
+ balance)
+ (define (call-the-cops arg)
+ "Calling the cops")
+ (define (error-msg arg)
+ "Incorrect Password")
+ (define (dispatch input-pass m)
+ (if (eq? input-pass password)
+ (begin
+ (cond
+ ((eq? m 'withdraw) withdraw)
+ ((eq? m 'deposit) deposit)
+ (else (error "Unknown request" m))))
+ (begin
+ (if (eq? incorrect-count 7)
+ (begin
+ (set! incorrect-count 0)
+ call-the-cops)
+ (begin
+ (set! incorrect-count (+ incorrect-count 1))
+ error-msg)
+ ))))
+ dispatch)
+
+;Usage:
+(define acc (make-accout 100 'admin))
+
+((acc 'admin 'withdraw) 50)
+;50
+((acc 'root 'deposit) 50)
+;"Incorrect Password"
+((acc 'root 'deposit) 50)
+((acc 'root 'deposit) 50)
+((acc 'root 'deposit) 50)
+((acc 'root 'deposit) 50)
+((acc 'root 'deposit) 50)
+((acc 'root 'deposit) 50)
+((acc 'root 'deposit) 50)
+((acc 'root 'deposit) 50)

0 comments on commit eb2964e

Please sign in to comment.