[Haskell-cafe] Lazy Lists and IO
Andrew Coppin
andrewcoppin at btinternet.com
Wed Jul 11 16:40:22 EDT 2007
Chaddaï Fouché wrote:
> 2007/7/11, Andrew Coppin <andrewcoppin at btinternet.com>:
>>
>> Ouch! That's gotta sting...
>>
>> I wasn't aware that this function was so leathal. I use it constantly
>> all the time...
>>
>
> It isn't that "lethal" usually. It's only because he was using it on
> an infinite stream that it hurt so much... If you use it on a normal
> stdin or a hGetContents on a file it will be fine (though you will
> lose the advantage of its laziness, for example constant memory
> treatment).
>
Loose lazyness?
Oh wait - you mean the inadvertent length thing?
We already have null, but how about a standard function in Data.List for
testing whether the length is longer than some upper limit?
lengthUpTo :: Int -> [x] -> Int
lengthUpTo n = length . take n
shorterThan :: Int -> Bool
shorterThan n = (n >) . lengthUpTo (n+1)
longerThan :: Int -> Bool
longerThan n = (n <) . lengthUpTo n
lengthEquals :: Int -> Bool
lengthEquals n = (n ==) . lengthUpTo (n+1)
(Perhaps need better names...)
More information about the Haskell-Cafe
mailing list