2012-11-08 75 views
0
 
(define-record-type car-ivars 
     (fields efficiency (mutable gas-in-tank))) 
(define-record-type car-methods 
     (fields drive! get-gas-in-tank refuel)) 
(define refuel (lambda (c g) ((car-methods-refuel c) g))) 
(define get-gas-level 
     (lambda (c) ((car-methods-get-gas-in-tank c)))) 
(define drive! 
     (lambda (c distance) ((car-methods-drive! c) distance))) 

(define make-car 
     (lambda (efficiency) 
     (let ([car1 (make-car-ivars efficiency 0)]) 
      (let ([set-gas-level! 
        (lambda (gas) (car-ivars-gas-in-tank-set! car1 gas))] 
       [gas-level 
        (lambda() ((car-ivars-gas-in-tank car1)))]) 
      (make-car-methods 
       ;;drive! 
       (lambda (distance) 
       (set-gas-level! 
       (- (get-gas-level) 
       (/ efficiency distance))) 
       ;;get-gas-level 
       (get-gas-levels) 
       ;;refuel 
       (lambda (gas1) 
       (set-gas-level! 
        (+ (get-gas-level) gas1))))))))) 

好吧,我有這個代碼。當我嘗試運行我的測試用例「面向對象」方案

(define hybrid (make-car 50))
時,出現
Exception: incorrect number of arguments to #procedure constructor
錯誤。我不太確定它是從哪裏來的。

+1

它會知道你正在使用哪個方案實施非常有用。 – Sebastian

+0

塞巴斯蒂安的觀點很好。我也認爲我們沒有足夠的代碼來說明問題;我懷疑這個錯誤實際上是在** make-car-ivars **或** make-car-methods **中生成的,但這純粹是沒有更多數據的推測。 – itsbruce

回答

0

您對make-car中的drive部分的定義缺少右括號,這意味着make-car-methods只能獲得一個參數。

重新縮進在Emacs使這個很明顯:

(define make-car 
    (lambda (efficiency) 
     (let ([car1 (make-car-ivars efficiency 0)]) 
      (let ([set-gas-level! 
        (lambda (gas) (car-ivars-gas-in-tank-set! car1 gas))] 
        [gas-level 
        (lambda() ((car-ivars-gas-in-tank car1)))]) 
       (make-car-methods 
       ;;drive! 
       (lambda (distance) 
        (set-gas-level! 
         (- (get-gas-level) 
         (/ efficiency distance))) 
        ;;get-gas-level 
        (get-gas-levels) 
        ;;refuel 
        (lambda (gas1) 
         (set-gas-level! 
          (+ (get-gas-level) gas1)))))))))