2011-10-12 52 views
5

你好我想创建一个嵌套,如果在lisp,但我们不断收到错误,我们不知道如何解决它!嵌套,如果在lisp

** - EVAL:特殊操作参数太多IF:

(defun spread-stones-helper(game-state StoneInHand Player player-index pit-index) 

    ;; Do we have more stones in our hand? 
    (if (> 0 StoneInHand) 
     ;; Are we above the pit limit? 
     (if (> pit-index 5) 
      ;; Switch the player and reset the pit-index to 0 
      (setq player-index (switchplayer player-index)) 
      (setq pit-index '0) 
     ) 

     ;; Add 1 to the pit 
     (set-pit game-state player-index (GetCorrectPit player-index pit-index) (+ (get-pit game-state player-index (GetCorrectPit player-index pit-index)) 1)) 

     ;; Recursive call the function, with one less stone and 1 up in pit-index 
     (spread-stones-helper game-state (- StoneInHand 1) Player player-index (+ pit-index 1)) 
    ) 
    ;; There are no more stones in hand, run capture stones 
    ;; (captureStones game-state StoneInHand Player player-index pit-index) 
) 
+0

我想你可能会让你自己感到困惑,因为你已经创建了奇怪的缩进/缩进样式。 – Ken

回答

8

在Lisp的if运营商接受三个表达式是条件,价值的情况下,条件为真值时,条件为假.. 。例如,

(if (< x 0) 
    (print "x is negative") 
    (print "x is greater or equal than zero")) 

您也可以省略最后一个表达式,在这种情况下,它假定为NIL。

如果你想要把更多的表现在两个案例之一,必须用他们的progn形式

(if (< x 0) 
    (progn 
     (print "HEY!!!!") 
     (print "The value of x is negative..."))) 

具有只有两个分支中的一个充满并与众多的if表达的情况下,表达式被发现是非常频繁的,并加入因此两个用于此确切使用特殊的变化:

(when (< x 0) 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing)) 

(unless (< x 0) 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing)) 

上述when形式等同于

(if (< x 0) 
    (progn 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing))) 

unless形式有很相同的含义,但随着病情逆转......换句话说,这相当于

(if (not (< x 0)) 
    (progn 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing))) 

总括来说,只有当你需要编写代码为你应该使用if分支(真和假)。否则,使用whenunless,这取决于您的测试更具可读性。

当使用if表格必须使用在你需要把多单的形式分支progn

5

不要忘记使用(progn ...)超过一个if语句

(defun spread-stones-helper (game-state StoneInHand Player 
          player-index pit-index) 

    ;; Do we have more stones in our hand? 
    (if (> 0 StoneInHand) 
     (progn 
     ;; Are we above the pit limit? 
     (if (> pit-index 5) 
     (progn 
       ;; Switch the player and reset the pit-index to 0 
       (setq player-index (switchplayer player-index)) 
       (setq pit-index '0))) 

     ;; Add 1 to the pit 
     (set-pit game-state player-index 
        (GetCorrectPit player-index pit-index) 
        (+ (get-pit game-state player-index 
           (GetCorrectPit player-index pit-index)) 
        1)) 

     ;; Recursive call the function, with one less stone and 1 
     ;; up in pit-index 
     (spread-stones-helper game-state 
           (- StoneInHand 1) 
           Player 
           player-index 
           (+ pit-index 1)))) 
    ;; There are no more stones in hand, run capture stones 
    ;; (captureStones game-state StoneInHand Player player-index pit-index) 
    ) 
5

“如果“需要测试和两种形式 -

你已经给了第一个”如果“一个测试和三表格

假设(> 0 StoneInHand)为真。

你想运行第二个if和set-pit语句吗?

如果是这样,你需要用它们在(progn这个)