首页 > 解决方案 > 在卫队haskell中使用case表达式时解析错误

问题描述

我是 Haskell 的新手,我遇到了语法问题。我想要做的是给定数据和这种数据类型的树,找到树中相应节点的路径。我相信我对该函数的逻辑是正确的,但我不知道如何使它成为有效的 Haskell。我尝试将制表符更改为空格。

-- | Binary trees with nodes labeled by values of an arbitrary type.
data Tree a
   = Node a (Tree a) (Tree a)
   | End
  deriving (Eq,Show)

-- | One step in a path, indicating whether to follow the left subtree (L)
--   or the right subtree (R).
data Step = L | R
  deriving (Eq,Show)

-- | A path is a sequence of steps. Each node in a binary tree can be
--   identified by a path, indicating how to move down the tree starting
--   from the root.
type Path = [Step]
pathTo :: Eq a => a -> Tree a -> Maybe Path
pathTo a End = Nothing
pathTo a (Node b l r)
    | a == b = Just []
    | case (pathTo a l) of
                Just p  -> Just [L:p]
                Nothing -> case (pathTo a r) of
                                    Just p  -> Just [R:p]
                                    Nothing -> Nothing

这是错误:

  parse error (possibly incorrect indentation or mismatched brackets)

标签: parsinghaskellfunctional-programmingsyntax-errorcase

解决方案



推荐阅读