I am following this Haskell tutorial and am on the higher order functions section. It defines a function called chain as:
chain :: (Integral a) => a -> [a]
chain 1 = [1]
chain n
| even n = n:chain (n `div` 2)
| odd n = n:chain (n * 3 + 1)
There is an exercise to find number of "chains" that have a length longer than 15. They do so like this:
numLongChains :: Int
numLongChains = length (filter isLong (map chain [1..100]))
where isLong xs = length xs > 15
I am trying to come up with a list comprehension that instead of giving me the number of chains gives me a list of chains that are longer than 15 from [1..100]. My closest attempt so far looks like:
[ [ a | a <- chain b, length a > 15] | b <- [1..100]]
but I get:
<interactive>:9:14:
No instance for (Integral [a0]) arising from a use of `chain'
Possible fix: add an instance declaration for (Integral [a0])
In the expression: chain b
In a stmt of a list comprehension: a <- chain b
In the expression: [a | a <- chain b, length a > 15]
<interactive>:9:45:
No instance for (Enum [a0])
arising from the arithmetic sequence `1 .. 100'
Possible fix: add an instance declaration for (Enum [a0])
In the expression: [1 .. 100]
In a stmt of a list comprehension: b <- [1 .. 100]
In the expression:
[[a | a <- chain b, length a > 15] | b <- [1 .. 100]]
<interactive>:9:46:
No instance for (Num [a0]) arising from the literal `1'
Possible fix: add an instance declaration for (Num [a0])
In the expression: 1
In the expression: [1 .. 100]
In a stmt of a list comprehension: b <- [1 .. 100]
Am I even close? I do want to solve this problem using a nested comprehension for the sake of learning despite the possible better ways to approach this.