莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理
              這一節(jié)主要是介紹局部狀態(tài)變量,介紹了set!和begin的語法,看來ruby使用!號來表示改變變量值不是什么新鮮主意。
            
          習(xí)題3.1,不解釋了
          ;習(xí)題3.1
          (define (make-accumulator init)
            (define (accumulator num)
              (set! init (+ init num))
              init)
            accumulator)

          習(xí)題3.2,非常有趣的例子,在內(nèi)部維持一個計數(shù)的變量即可,如果傳入的參數(shù)是特定的符號就返回計數(shù)或者清0,如果不是,原過程調(diào)用。

          ;習(xí)題3.2
          (define (make-monitored proc)
             (let ((counter 0))
            (define (proc-monitor args)
                   (cond ((eq? args 'how-many-calls?) counter)
                      ((eq? args 'reset-count) (begin (set! counter 0) counter))
                      (else
                        (begin (set! counter (+ counter 1)) (proc args)))))
            proc-monitor))

          請注意,我的實現(xiàn)只能針對有一個參數(shù)的過程,對于多個參數(shù)的過程我還不知道怎么做。

          習(xí)題3.3,passwd的局部狀態(tài)變量,在dispatch前比較下傳入的密碼是否與之一致

          ;習(xí)題3.3
          (define (make-account balance passwd)
            (define (withdraw amount)
              (if (>= balance amount)
                  (begin (set! balance (- balance amount)) balance)
                  "余額不足"))
            (define (deposit amount)
              (set! balance (+ balance amount))
              balance)
            (define (dispatch pwd m)
              (if (eq? pwd passwd)
                  (cond ((eq? m 'withdraw) withdraw)
                        ((eq? m 'deposit) deposit)
                      (else
                         (error "Unknow request--MAKE-ACCOUNT" m)))
                  (lambda(x) "Incorrect password")))
                  
            dispatch)
          不一致的時候,返回一個匿名過程,僅僅是輸出消息Incorrect password

          習(xí)題3.4,在內(nèi)部維持一個局部變量counter,用于計數(shù)密碼錯誤的次數(shù),在dispatch前判斷counter是否等于7,如果是7就調(diào)用過程call-the-cops。

          ;習(xí)題3.4
          (define (make-account balance passwd)
            (let ((counter 0))
            (define (withdraw amount)
              (if (>= balance amount)
                  (begin (set! balance (- balance amount)) balance)
                  "余額不足"))
            (define (deposit amount)
              (set! balance (+ balance amount))
              balance)
            (define (call-the-cops amount)
              "您已經(jīng)嘗試輸入密碼7次了!不能再試!")
            (define (dispatch pwd m)
              (cond ((= 7 counter) call-the-cops)
                    ((eq? pwd passwd)
                     (cond ((eq? m 'withdraw) withdraw)
                           ((eq? m 'deposit) deposit)
                           (else
                             (error "Unknow request--MAKE-ACCOUNT" m))))
                    (else
                      (begin (set! counter (+ counter 1)) (lambda(x) "Incorrect password")))))
           dispatch))




          主站蜘蛛池模板: 乐至县| 奎屯市| 邵武市| 日喀则市| 东宁县| 永清县| 巫溪县| 望都县| 醴陵市| 大丰市| 孟州市| 昭通市| 台中市| 体育| 开鲁县| 湖北省| 衡阳市| 济宁市| 织金县| 年辖:市辖区| 龙口市| 武鸣县| 远安县| 中江县| 鄄城县| 翁牛特旗| 滦南县| 平乐县| 永泰县| 石河子市| 鲁甸县| 定州市| 娄底市| 大兴区| 罗田县| 南丹县| 晋州市| 福州市| 蒙阴县| 兴文县| 格尔木市|