Nested if in lisp

Hello, I'm trying to create a nested if in lisp, but we keep getting the error, and we don’t know how to fix it!

** - EVAL: too many parameters for the special IF statement:

(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)
)
+5
source share
3 answers

In lisp, an operator ifaccepts three expressions that are a condition, a value if the condition is true and a value when the condition is false ... for example

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

You can also omit the last expression, in which case it is considered NIL.

If you want to add more expressions in one of two cases, you must wrap them in a form 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 , ( ). when unless , .

if progn , .

+8

(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

"if" -

""

, ( > 0 StoneInHand) .

if set-pit?

, (progn)

+5

All Articles