I’m very new with Haskell, only starting to learn it.
I’m using “Learn You a Haskell for Great Good!” tutorial for start, and saw example of solving “3n+1” problem:
chain :: (Integral a) => a -> [a]
chain 1 = [1]
chain n
| even n = n:chain (n `div` 2)
| odd n = n:chain (n*3 + 1)
numLongChains :: Int
numLongChains = length (filter isLong (map chain [1..100]))
where isLong xs = length xs > 15
so, numLongChains counts all chains that longer 15 steps, for all numbers from 1 to 100.
Now, I wanna my own:
numLongChains' :: [Int]
numLongChains' = filter isLong (map chain [1..100])
where isLong xs = length xs > 15
so now, I wanna not to count these chains, but return filtered list with these chains.
But now I get error when compiling:
Couldn't match expected type `Int' with actual type `[a0]' Expected type: Int -> Bool Actual type: [a0] -> Bool In the first argument of `filter', namely `isLong' In the expression: filter isLong (map chain [1 .. 100])
What can be the problem?
The type signature of
numLongChainsis probably not correct. Depending on what you want to do, one of the following is needed:numLongChainsobviously shall return a number, change the first line tolength $ filter isLong (map chain [1..100])and the type toIntfilter (>15) (map (length . chain) [1..100]).[[Int]](A list of chains (lists) ofInts) and you’re fine.