[Haskell-cafe] Lazy Lists and IO

Stefan O'Rear stefanor at cox.net
Wed Jul 11 17:53:00 EDT 2007


On Wed, Jul 11, 2007 at 11:46:10PM +0200, Chaddaï Fouché wrote:
> 2007/7/11, Stefan O'Rear <stefanor at cox.net>:
>>
>> Interestingly, the function is already there; it's called genericLength.
>>
>> However, the lazy natural type isn't.
>
> I'm not sure what you mean there : genericLength is just a length that
> can return any Num (eg. an Integer) and not just Int, it has nothing
> to do with what Andrew wanted, which was a function that checked if a
> list was longer than n without swallowing more of the list than
> necessary.
> Is there something I misunderstood in the exchange ?

stefan at stefans:/tmp$ cat Z.hs
import List

data Nat = Z | S Nat deriving(Eq, Ord, Show)

instance Num Nat where
    Z + x = x
    S x + y = S (x + y)

    Z * x = Z
    S x * y = y + (x * y)

    fromInteger 0 = Z
    fromInteger (n+1) = S (fromInteger n)

main = print $ genericLength (1 : 2 : undefined) > (1 :: Nat)
stefan at stefans:/tmp$ runghc Z.hs 

Z.hs:5:0:
    Warning: No explicit method nor default method for `abs'
    In the instance declaration for `Num Nat'

Z.hs:5:0:
    Warning: No explicit method nor default method for `signum'
    In the instance declaration for `Num Nat'
True
stefan at stefans:/tmp$ 

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070711/5d755568/attachment.bin


More information about the Haskell-Cafe mailing list