嵌套在lisp中

And*_*ome 5 lisp

您好我正在尝试在lisp中创建嵌套if,但我们不断收到错误,我们不知道如何解决它!

** - 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)
)
Run Code Online (Sandbox Code Playgroud)

650*_*502 8

在lisp中,if运算符接受三个表达式,条件是条件,值是条件为真,值是条件为假值...例如

(if (< x 0)
    (print "x is negative")
    (print "x is greater or equal than zero"))
Run Code Online (Sandbox Code Playgroud)

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

如果要在两种情况之一中添加更多表达式,则必须将它们包装在progn表单中

(if (< x 0)
    (progn
       (print "HEY!!!!")
       (print "The value of x is negative...")))
Run Code Online (Sandbox Code Playgroud)

具有的情况下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))
Run Code Online (Sandbox Code Playgroud)

when上面的表格相当于

(if (< x 0)
   (progn
     (do-this)
     (do-that)
     (do-even-that-other-thing)))
Run Code Online (Sandbox Code Playgroud)

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

(if (not (< x 0))
   (progn
     (do-this)
     (do-that)
     (do-even-that-other-thing)))
Run Code Online (Sandbox Code Playgroud)

回顾一下,if只有在需要为两个分支(真假分支)编写代码时才应该使用.否则使用其中之一whenunless取决于您的测试更具可读性.

使用if表单时,您必须使用prognin分支,您需要在其中放置多个表单.


the*_*olm 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)
   )
Run Code Online (Sandbox Code Playgroud)


小智 5

"如果"进行测试和两种形式 -

您已经给出了第一个"if"测试和三个表单

假设(> 0 StoneInHand)是真的.

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

如果是这样,你需要将它们包装成(预测)