球拍 - 功能调用错误

时间:2015-11-20 00:26:17

标签: racket

定义:

(define-struct Cell (x y))

(define (count-in cell cells)
(cond
[(member? cell cells) 1]
[else 0]))

(define (touches cell cells)
(+
(count-in
(cell (Cell-x cell) (+ (Cell-y cell) 1)) (list cells))
(count-in
(cell (Cell-x cell) (+ (Cell-y cell) -1))(list cells))
(count-in
(cell (+ (Cell-x cell) -1) (Cell-y cell))(list cells))
(count-in
(cell (+ (Cell-x cell) 1) (Cell-y cell))(list cells))))

检查预期以及错误发生的位置:

(check-expect (touches (make-Cell 2 30) 
(list
make-Cell 2 31
make-Cell 1 29
make-Cell 1 30
make-Cell 2 30)) 2)

它给我一个错误说:

函数调用:预期开括号后的函数,但收到(make-Cell 2 30)

为什么会这样?我将如何解决此问题?

代码的作用:检查一个单元格(其值调整为1)目前是否在列表中。所以make-cell 2 30,调整值就是 2 31,2 29,1 30,30 30.如果其中一个单元格在列表中,它将产生一个1,如果不是它将产生一个0.然后如果代码只是将它们全部加起来告诉我在列表中有多少调整后的细胞。

1 个答案:

答案 0 :(得分:1)

您忘记用括号括起make-cell的来电,touches程序看起来不对。假设member?过程正确实现并适用于结构列表,这应该有效:

(define (touches cell cells)
  (+
   (count-in
    (make-Cell (Cell-x cell) (+ (Cell-y cell) 1))
    cells)
   (count-in
    (make-Cell (Cell-x cell) (+ (Cell-y cell) -1))
    cells)
   (count-in
    (make-Cell (+ (Cell-x cell) -1) (Cell-y cell))
    cells)
   (count-in
    (make-Cell (+ (Cell-x cell) 1) (Cell-y cell))
    cells)))

(check-expect
 (touches (make-Cell 2 30) 
          (list
           (make-Cell 2 31)
           (make-Cell 1 29)
           (make-Cell 1 30)
           (make-Cell 2 30)))
 2)