I have a function fromRange which takes a filter function and an interval and returns a set with all elements in the interval that satisfy the filter function.
I implemented it using list comprehension:
fromRange :: (Integer->Bool) -> (Integer,Integer) -> [Integer]
fromRange f (x,y) = [i | i<-[x..y], f i]
but it takes very long time with big list so I found the lazy evaluation concept but I do not know how exactly to implement it, any help??
Lazy evaluation is not supposed to be faster for large datasets, it only postpones the evaluation until the very moment when the value is needed. For example, if you type in your
ghci:you’ll have to wait forever before it iterates through the whole list to filter it and print the result.
On the other hand:
will complete instantly, because it does not have to calculate the rest of the list.
Note:
take 100will hang as well, since it won’t ever find enough entries.