2013-04-27 61 views
1

我試圖通過使用方案來實現一個博弈論算法。我寫了一個代碼片,命名爲兩個tat的tit。下面是代碼:如何在計劃中使用「cond」?

(define (tit-for-two-tat my-history other-history) 
(cond ((empty-history? my-history) 'c) 
    ((= 'c (most-recent-play other-history)) 'c) 
    ((= 'c (second-most-recent-play other-history)) 'c) 
    (else 'd))) 

我也試着寫這樣的:

(define (tit-for-two-tat my-history other-history) 
(cond ((empty-history? my-history) 'c) 
    ((= 'c (or (most-recent-play other-history) (second-most-recent-play other-history))) 'c) 
    (else 'd))) 

遊戲的情況是「囚徒困境」。 c表示座標d表示缺陷。當我嘗試運行此代碼它給兩個類型代碼以下錯誤:

expects type <number> as 1st argument, given: 'c; other arguments were: 'c 

我通過給這個函數作爲參數傳遞給函數「發揮環」運行它。遊戲循環給了我。

可能是什麼問題?謝謝你的幫助。

回答

2

您正在調用=功能符號'c,但=需要一個數字。它看起來像eq?將是你的等價性檢查的正常功能。

+0

是真的謝謝:) – user2870 2013-04-27 15:18:55

1

你對'c,這是一個符號比較 - 那麼你必須使用eq?是否相等的比較。或者更一般的平等的測試過程中,使用equal?,這將適用於大多數的數據類型(字符串,數字,符號等),特別是:

(define (tit-for-two-tat my-history other-history) 
    (cond ((empty-history? my-history) 'c) 
     ((equal? 'c (most-recent-play other-history)) 'c) 
     ((equal? 'c (second-most-recent-play other-history)) 'c) 
     (else 'd)))