如何在计划中使用“cond”?

时间:2013-04-27 14:30:33

标签: scheme racket

我正在尝试使用方案实现博弈论算法。我写了一个代码片,名为tit for two tat。这是代码:

(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

我通过将此函数作为参数添加到“play-loop”函数来运行它。播放循环给了我。

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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)))