learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-4.scm (1194B)


      1 (define-library (sicp solutions chapter-3 exercise-4)
      2   (import (scheme base))
      3   (export make-account)
      4 
      5   (begin
      6     (define (call-to-cops)
      7       "Cops called.")
      8 
      9     (define (make-account balance super-secret-symbol)
     10       (define number-of-failed-authentication-attempts 0)
     11       
     12       (define (withdraw amount)
     13         (if (>= balance amount)
     14             (begin
     15               (set! balance
     16                     (- balance
     17                        amount))
     18               balance)
     19             "Insufficient funds"))
     20 
     21       (define (deposit amount)
     22         (set! balance (+ balance
     23                          amount))
     24         balance)
     25 
     26       (lambda (whisper m)
     27         (if (eq? whisper super-secret-symbol)
     28             (cond
     29              ((eq? m 'withdraw) withdraw)
     30              ((eq? m 'deposit) deposit)
     31              (else (error "Uknown request: MAKE-ACCOUNT" m)))
     32             (lambda x
     33               (begin
     34                 (set! number-of-failed-authentication-attempts
     35                       (+ 1 number-of-failed-authentication-attempts))
     36                 (if (<= 7 number-of-failed-authentication-attempts)
     37                     (call-to-cops)
     38                     "Incorrect password"))))))))