2010-12-03 42 views
0

我今天发布了一个问题,有人帮助,但他的代码不工作。这是链接到后link to the post需要帮助修复一个小问题

当我运行他的代码,我得到一个错误:“输入`解析错误'”

的“。”之后“\ t”导致错误。在Haskell中是新的,所以我不知道它为什么会导致错误。

需要帮助解决此问题,但。

感谢

UPDATE

我得到的错误,当我加载代码。这是错误:

Couldn't match expected type `a -> Bool' 
      against inferred type `[Bool]' 
    In the first argument of `any', namely 
     `(map (\ t -> contains t b) c)' 
    In the second argument of `(||)', namely 
     `any (map (\ t -> contains t b) c)' 
    In the expression: a == b || any (map (\ t -> contains t b) c) 

需要帮助修复它。

感谢

UPDATE

当我进行更改后,运行该代码:

data Tree a = Leaf a | Branch a [(Tree a)] deriving (Show) 

contains :: Tree a -> a -> Bool 
contains (Leaf a) b = a == b 
contains (Branch a c) b = a == b or (map (\ t -> contains t b) c) 

这是错误我现在得到:

Occurs check: cannot construct the infinite type: 
     a = ([Bool] -> Bool) -> [Bool] -> a 
    When generalising the type(s) for `contains' 

更新3

请这是我的代码,但我做了所有更改后仍然出现错误。我不明白:

data Tree a = Leaf a | Branch a [(Tree a)] deriving (Show) 

contains :: Eq a => Tree a -> a -> Bool 
contains (Leaf a) b = a == b 
contains (Branch a c) b = a == b || (map (\t -> contains t b) c) 

布尔不能在我的系统上工作,所以我必须使用布尔。现在这是我得到的错误:

Couldn't match expected type `Bool' against inferred type `[Bool]' 
    In the second argument of `(||)', namely 
     `(map (\ t -> contains t b) c)' 
    In the expression: a == b || (map (\ t -> contains t b) c) 
    In the definition of `contains': 
     contains (Branch a c) b = a == b || (map (\ t -> contains t b) c) 

如果它能工作,请运行上面的代码。或者,也许我错过了某个地方。 我非常感谢你的时间。我很感激。

感谢

+0

这是一个错字,methinks(`\ t。(...)`不是有效的Haskell,`\ t - >(...)` - 用有效的替换省略号,当然),他已经修好了。 – delnan 2010-12-03 15:54:25

回答

2

从它的身体在Haskell分离拉姆达的参数列表的语法->,不.像演算。

顺便说一句:这已经在您链接到的帖子中修复。

编辑:他还得到了any的用法错误 - 他把它当作是or。它需要要么

or (map (\ t -> contains t b) c) 

any (\t -> contains t b) c 

除了这种类型的签名是错误的(Boolean而不是Bool和失踪Eq约束)。所以,要么删除,或将其更改为:

contains :: Eq a => Tree a -> a -> Bool 
+0

感谢您的回复。我也修复了它,但是当我运行代码时出现新的错误。我在上面的帖子中包含了这个新错误。它与类型有关。需要帮助解决它。 – kap 2010-12-03 16:09:52