Reputation: 159
I have this code:
trimorficos :: [Integer]
trimorficos = filter (trim) [0..]
trim :: Integer -> Bool
trim x = (show x) `isSuffixOf` (show (x^3))
where a = show x
b = show (x^3)
densityTrimorficos :: Integer -> Double
densityTrimorficos n = fromInteger (n - (genericLength (filter (<=10) trimorficos))) / fromInteger n
Why the last function densityTrimorficos
doesn't work?
Upvotes: 0
Views: 56
Reputation: 116139
trimorficos
is an infinite list.
filter (<=10) trimorficos
will never produce the end-of-list []
at the very end. To do so, it should verify that, from a certain point onward, trimorficos
contains only numbers >10, but that would require infinite time.
Basically, filter
will returns something like a:b:c:nonTerminating
instead of a:b:c:[] = [a,b,c]
.
Consequently, genericLength
fails to terminate, since it tries to evaluate nonTerminating
into either d:...
or []
but that requires infinite time.
As pointed out above in the comments, you probably want takeWhile (<=10) trimorficos
instead, which will produce []
as soon as the first >10 number is encountered. Note that this will not check the rest of the list, unlike filter
.
Upvotes: 1