【问题标题】:haskell minimax / nested conditions and whereshaskell minimax / 嵌套条件和 wheres
【发布时间】:2014-09-24 12:20:39
【问题描述】:

对于一项任务,我需要在提供给该功能的游戏树上编写一个 Minimax 函数(作为棋盘树;玫瑰棋盘)和轮到它的玩家。但是,我收到有关输入“|”上的解析错误的错误。可能是因为我嵌套了条件和 where 语句,但我不确定我是否正确地做到了这一点,或者这是否可能(或者应该以不同的方式完成):

minimax :: Player -> Rose Board -> Rose Int --Rose Int = Int :> [Rose Ints]
minimax p rb = minimax' rb p
          where minimax' (b :> [rbs]) p0 | null rbs  = result
                                                where result | p0 == p   =  1
                                                             | otherwise = -1
                                         | otherwise = 0 :> (minimax' rbs (nextPlayer p0))

如果有人可以帮助我,非常感谢!

最好的问候, Skyfe。

【问题讨论】:

  • 你必须将内部where移到警卫之后。

标签: haskell nested minimax


【解决方案1】:

解决此问题的最简单方法可能是使用let 而不是where

minimax :: Player -> Rose Board -> Rose Int --Rose Int = Int :> [Rose Ints]
minimax p rb = minimax' rb p
          where minimax' (b :> [rbs]) p0 | null rbs  = let result | p0 == p   =  1
                                                                  | otherwise = -1
                                                       in result
                                         | otherwise = 0 :> (minimax' rbs (nextPlayer p0))

但你也可以只使用条件表达式:

minimax :: Player -> Rose Board -> Rose Int --Rose Int = Int :> [Rose Ints]
minimax p rb = minimax' rb p
          where minimax' (b :> [rbs]) p0 | null rbs  = if p0 == p then 1 else -1
                                         | otherwise = 0 :> (minimax' rbs (nextPlayer p0))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2013-08-04
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多