[Haskell-cafe] Odd lack of laziness
Neil Mitchell
ndmitchell at gmail.com
Sat Jun 23 10:22:32 EDT 2007
Hi
I have most of a Data.Nat library done, I should finish it off and release it...
Thanks
Neil
On 6/22/07, Stefan O'Rear <stefanor at cox.net> wrote:
> On Fri, Jun 22, 2007 at 07:14:39PM +0100, Andrew Coppin wrote:
> > Chaddaï Fouché wrote:
> > >You should be using BS.null f rather than BS.length f > 0.
> > >
> > While we're on the subject... anybody know a neat way to check, say,
> > whether a list contains exactly 1 element? (Obviously pattern matching
> > can do it, but that requires big case-expressions...)
>
> data LazyNat = Zero | Succ LazyNat deriving(Eq,Ord)
>
> instance Enum LazyNat where
> succ = Succ
> pred (Succ x) = x
>
> toEnum 0 = Zero
> toEnum (x+1) = succ (toEnum x)
>
> fromEnum Zero = 0
> fromEnum (Succ x) = fromEnum x + 1
>
> instance Num LazyNat where -- this is a lie, the lifted naturals only
> -- form a *semi*ring. Sigh.
> fromIntegral = toEnum
>
> Zero + y = y
> Succ x + y = Succ (x + y)
>
> Zero * y = 0
> Succ x * y = y + x * y
>
> abs = id
> signum 0 = 0
> signum _ = 1
>
> x - Zero = x
> Succ x - Succ y = x - y
>
>
> length' [] = Zero
> length' (x:xs) = Succ (length xs)
>
> null x = length' x == 0
>
> one x = length' x == 1
>
> atLeastFive x = length' x >= 5
>
> Stefan
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list