<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    莊周夢蝶

    生活、程序、未來
       :: 首頁 ::  ::  :: 聚合  :: 管理
        這一節(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))

    請注意,我的實(shí)現(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))




    主站蜘蛛池模板: 在线亚洲高清揄拍自拍一品区| 亚洲免费在线观看| 亚洲综合一区二区精品导航| 国产精品免费久久久久影院| 免费a级毛片网站| 国产精品亚洲精品爽爽| 色吊丝最新永久免费观看网站| 亚洲欧美国产精品专区久久| 免费看片免费播放| 亚洲.国产.欧美一区二区三区| 免费国产不卡午夜福在线| 麻豆91免费视频| 亚洲精品成人在线| 国产一级一毛免费黄片| 亚洲av无码潮喷在线观看 | 久久精品国产亚洲77777| 久久国产乱子免费精品| 亚洲综合色丁香麻豆| 永久免费的网站在线观看| 亚洲色大18成人网站WWW在线播放| 免费涩涩在线视频网| 男女猛烈激情xx00免费视频| 亚洲色偷偷综合亚洲AVYP| 午夜影院免费观看| 亚洲国产视频久久| 亚洲狠狠爱综合影院婷婷| 日本在线免费播放| 亚洲一区二区无码偷拍| 亚洲精品无码AV中文字幕电影网站| a毛看片免费观看视频| 亚洲毛片一级带毛片基地| 午夜免费福利在线观看| www免费插插视频| 亚洲天堂一区二区| 日日操夜夜操免费视频| 中文字幕在线免费播放| 亚洲一级大黄大色毛片| 亚洲乱码日产精品a级毛片久久| 久久黄色免费网站| 亚洲AV成人精品一区二区三区| 国产aⅴ无码专区亚洲av麻豆|