[Haskell-cafe] Odd lack of laziness
Stefan O'Rear
stefanor at cox.net
Fri Jun 22 14:26:48 EDT 2007
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
More information about the Haskell-Cafe
mailing list