2533: Generic functions that take integral arguments should work the same way as their prelude counterparts

kahl at cas.mcmaster.ca kahl at cas.mcmaster.ca
Sun Aug 24 00:25:36 EDT 2008


 > 
 > Can you examples of both naive definitions and fast definitions of Nat? I'm
 > curious.

Naive:

 > data Natural' = Zero | Succ Natural'

Fast:

 > type Nat = Word64

 (or Word if you want to retrofit to Haskell-1.5, a.k.a. Haskell98 ;-).


 > newtype Natural = N Integer -- to be exported abstractly
 > 
 > instance Num Natural where
 >   ...
 >   N a - N b =  let d = a - b in if d >= 0 then N d
 >                else error "Illegal Natural subtraction"
 >                -- one argument against Num ;-)
 > 
 > subtract (N a) (N b) =  let d = a - b in if d >= 0 then Just $ N d
 >                         else Nothing

....



Wolfram


More information about the Libraries mailing list