Since natural numbers are trivial to implement (inefficiently) I took 15 minutes and added them to my numbers package in Hackage.<br><a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/numbers-2007.9.24">http://hackage.haskell.org/cgi-bin/hackage-scripts/package/numbers-2007.9.24
</a><br><br> -- Lennart<br><br><div><span class="gmail_quote">On 9/24/07, <b class="gmail_sendername">Andrew Coppin</b> <<a href="mailto:andrewcoppin@btinternet.com">andrewcoppin@btinternet.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Neil Mitchell wrote:<br>> Hi<br>><br>><br>>>> Pretty much, yes.<br>>>><br>>>><br>>> So I just need to write<br>>><br>>> newtype LazyNatural = LazyNatural [()]<br>>>
<br>><br>> or<br>><br>> data Nat = Zero | Succ Nat<br>><br>> it's your choice really.<br>><br><br>I'm guessing there's going to be fairly minimal performance difference.<br>(Or maybe there is. My way uses a few additional pointers. But it also
<br>allows me to elegantly recycle existing Prelude list functions, so...)<br><br>>> and then add some suitable instances. ;-)<br>>><br>><br>> Yes. Lots of them. Lots of instances and lots of methods.<br>
><br>><br>>> Hey, the "length" function would then just be<br>>><br>>> ln_length :: [x] -> LazyNatural<br>>> ln_length = LazyNatural . map (const ())<br>>><br>>> Ooo, that's hard.
<br>>><br>><br>> Nope, its really easy. Its just quite a bit of work filling in all the<br>> instances. I bet you can't do it and upload the results to hackage<br>> within 24 hours :-)<br>><br><br>
*ALL* the instances? No.<br><br>A small handful of them? Sure. How about this...<br><br><br><br>module LazyNatural (LazyNatural ()) where<br><br>import Data.List<br><br>newtype LazyNatural = LN [()]<br><br>instance Show LazyNatural where
<br> show (LN x) = "LN " ++ show (length x)<br><br>instance Eq LazyNatural where<br> (LN x) == (LN y) = x == y<br><br><br>instance Ord LazyNatural where<br> compare (LN x) (LN y) = raw_compare x y<br><br>raw_compare ([]) (_:_) = LT
<br>raw_compare ([]) ([]) = EQ<br>raw_compare (_:_) ([]) = GT<br>raw_compare (_:x) (_:y) = raw_compare x y<br><br><br>instance Num LazyNatural where<br> (LN x) + (LN y) = LN (x ++ y)<br> (LN x) - (LN y) = LN (raw_minus x y)
<br> (LN x) * (LN y) = LN (concatMap (const x) y)<br> negate _ = error "negate is not defined for LazyNatural"<br> abs = id<br> signum (LN []) = LN []<br> signum (LN _) = LN [()]<br> fromInteger = LN . flip genericReplicate ()
<br><br>raw_minus (_:a) (_:b) = raw_minus a b<br>raw_minus (a) ([]) = a<br>raw_minus _ _ = error "negative result from subtraction"<br><br>_______________________________________________<br>Haskell-Cafe mailing list
<br><a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></blockquote></div><br>