[Haskell-cafe] Re: Maybe and partial functions
Neil Mitchell
ndmitchell at gmail.com
Tue Mar 13 06:17:05 EDT 2007
Hi
> -- There are no pattern-matching failures here.
> -- The totality is harder to see: all digits are roughly of the same range,
> -- but each recursive call increments base. Eventually, base becomes bigger
> -- than d+9 and so the first alternative will be selected, which is in the
> -- WHNF and so recursion terminates.
> carryPropagate base (C d ds)
> | carryguess == (d+9) `div` base
> = C carryguess (C (remainder+nextcarry) fraction)
> | otherwise
> = (C (dCorrected `div` base) (C (dCorrected `mod` base) fraction))
> where carryguess = d `div` base
> remainder = d `mod` base
> C nextcarry fraction = carryPropagate (base+1) ds
> dCorrected = d + nextcarry
>
> e = map (show.head) $
> iterate (carryPropagate 2 . map (10*) . tail) $
> C 2 (rept 1)
>
> en n = "2." ++ concat (take n (tail e))
You still have div and mod, which are partial functions.
Thanks
Neil
More information about the Haskell-Cafe
mailing list