2013-04-27 67 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)))