Ethiopian multiplication: Difference between revisions
m
→Haskell :: Fold after unfold
(→Haskell :: Fold after unfold: Tidied and retitled) |
|||
Line 2,117:
Logging the stages of the '''unfoldr''' and '''foldr''' applications:
<lang haskell>import Data.
import Data.Tuple (swap)
import Debug.Trace (trace)
Line 2,126 ⟶ 2,125:
ethMult :: Int -> Int -> Int
ethMult n m =
trace
(showDoubles pairs <> "
| otherwise = a▼
where
pairs = zip (unfoldr
doubled x = x +
halved
| 0 < h
v = foldr addedWhereOdd 0
| 0 < d = (+) a
| otherwise =
▲ )
---------------------- TRACE DISPLAY ---------------------
showHalf :: Int -> String
showHalf x = "halve: " <> rjust 6 ' ' (show (quotRem x 2))
Line 2,155 ⟶ 2,151:
showDoubles xs =
"double:\n"
<> unlines (go <$> xs)
<> intercalate " + " (xs >>=
where
go
| 0 < fst x = "-> " <>
| otherwise
| 0 < r
| otherwise =
rjust :: Int -> Char -> String -> String
rjust n c s = drop (length s) (replicate n c <> s)
--------------------------- TEST -------------------------
|