Song's profileSICP自留地BlogListsNetwork Tools Help

Blog


    July 28

    sicp 3.7

    ;不难看出make-joint需要做的是在客户账户中添加一条额外的密钥并返回该账户
    ;由于帐户这个对象在系统中是以过程形式定义的,不调用过程无法修改其本地参数
    ;有必要在make-account中添加额外的命令来收集新密钥
    (define (make-account balance password);初始化时以list形式输入password
      (define (withdraw amount)
        (if (>= balance amount)
            (begin (set! balance (- balance amount))
                   balance)
            "Insufficient funds"))
      (define (deposit amount)
        (set! balance (+ balance amount))
        balance)
      (define (in? p password1)
              (cond ((null? password1) #f)
                    ((eq? p (car password1)) #t)
                    (else (in? p (cdr password1)))))
      (define (addpassword password1)
              (set! password (cons password1 password)))
                    
      (define (dispatch p m)
        (if (in? p password);测试p是否在password列表中
            (cond ((eq? m 'withdraw) withdraw)
                  ((eq? m 'deposit) deposit)
                  ((eq? m 'addpassword) addpassword)
                  (else (error "Unknown request -- MAKE-ACCOUNT"
                               m)))
            (error "Incorrect password" m))
        )
      dispatch)
    (define (make-joint account oldpassword newpassword)
      (begin ((account oldpassword 'addpassword) newpassword) account))

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://mysicp.spaces.live.com/blog/cns!76113039590E11D7!162.trak
    Weblogs that reference this entry
    • None