2013-04-27 2 views
1

체계를 사용하여 게임 이론 알고리즘을 구현하려고합니다. 나는 두 tat에 대한 tit라는 코드 조각을 썼다."cond"in scheme을 사용하는 방법?

(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

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))) 
관련 문제