functional programming - Haskell - what is wrong with this function? -


i trying compute harmonic series function below. there's type error , not quite sure mean? question, why [5..1] gives empty list?

sumhr = foldr (+) 0 (\x -> map (1/) [1..x])

error message:

*** expression     : foldr (+) 0 (\x -> map (1 /) (enumfromto x 1)) *** term           : \x -> map (1 /) (enumfromto x 1)     *** type           : b -> [b]     *** not match : [a]     

the error telling code not well-typed , doesn't make sense.

your function:

sumhr = foldr (+) 0 (\x -> map (1/) [1..x]) 

consider:

prelude> :t foldr foldr :: (a -> b -> b) -> b -> [a] -> b 

so true, (+) first argument , types must unify (a -> b -> b , num => -> -> a unify num => -> -> a).

the second argument given type variable b, know must num => a. fine, have provided 0 second argument.

the third argument must agree type num => [a]. however, have provided second argument function:

prelude> :t (\x -> map (1/) [1..x]) (\x -> map (1/) [1..x]) :: (enum b, fractional b) => b -> [b] 

unless can show compiler how type of (enum b, fractional b) => b -> [b] can made same num => [a] stuck.

you might have ment function such as:

sumhr x = foldr (+) 0 (map (1/) [1..x]) 

Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -