1

我正在尝试通过使用方案来实现博弈论算法。我写了一段代码,名为“一报还一报”。这是代码:

(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

我通过将此函数作为“播放循环”函数的参数来运行它。播放循环给了我。

可能是什么问题?谢谢你的帮助。

4

2 回答 2

2

您正在=对符号调用该函数,'c=需要一个数字。看起来这eq?将是您的等价检查的正确功能。

于 2013-04-27T14:46:51.193 回答
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)))
于 2013-04-27T15:12:46.330 回答