开发者

Need Help Fixing A little Bug

开发者 https://www.devze.com 2023-01-29 05:07 出处:网络
I posted a question today and someone helped but his code is not working. This is the link to the post link to the post

I posted a question today and someone helped but his code is not working. This is the link to the post link to the post

When i run his code, i get an error : "parse error on input `.'"

The "." after the "\t" is causing the error. Am new in Haskell so i don't know why it is causing the error.

Need help in resolving this but.

thanks

UPDATE

I get error when i load the code. this is the error:

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)

need help in fixing it.

thanks

UPDATE

when i run the code after making the changes:

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)

this is the error i now get:

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

UPDATE 3

Please this is my code and yet i still get error after making all t开发者_开发技巧he changes. I don't understand:

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)

The Boolean doesn't work on my system so i have to use Bool. Now this is the error i get:

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)

Will you please run the above code if it will work. Or maybe am missing something somewhere. I want to thank you soo much for your time. I appreciate it.

thanks


The syntax to separate a lambda's argument list from its body in Haskell is ->, not . like in lambda calculus.

Btw: this has already been fixed in the post you linked to.

Edit: He also got the usage of any wrong - he's treating it as if it were or. It needs to be either

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

or

any (\t -> contains t b) c

In addition to this the type signature is wrong (Boolean instead of Bool and missing Eq constraint). So either remove it or change it to:

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


Surprisingly, the error you get tells you exactly what is wrong: You get a list of Bools on the right side of ||. So you need to process the result list from the map further, e.g. by applying and or or (probably the latter one). See http://hackage.haskell.org/packages/archive/haskell98/latest/doc/html/List.html#v:or

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号