Ethiopian multiplication: Difference between revisions

Content added Content deleted
(→‎Haskell :: Fold after unfold: Tidied and retitled)
Line 2,117: Line 2,117:
Logging the stages of the '''unfoldr''' and '''foldr''' applications:
Logging the stages of the '''unfoldr''' and '''foldr''' applications:


<lang haskell>import Data.Bool (bool)
<lang haskell>import Data.List (inits, intercalate, unfoldr)
import Data.List (intercalate, unfoldr)
import Data.Tuple (swap)
import Data.Tuple (swap)
import Debug.Trace (trace)
import Debug.Trace (trace)
Line 2,126: Line 2,125:
ethMult :: Int -> Int -> Int
ethMult :: Int -> Int -> Int
ethMult n m =
ethMult n m =
trace
let addedWhereOdd (d, x) a
| 0 < d = (+) a x
(showDoubles pairs <> " = " <> show v <> "\n")
v
| otherwise = a
where
halved h
| 0 < h =
pairs = zip (unfoldr halved n) (iterate doubled m)
Just $
doubled x = x + x
trace
halved h
(showHalf h)
| 0 < h =
(swap $ quotRem h 2)
Just $
| otherwise = Nothing
trace
doubled x = x + x
(showHalf h)
pairs = zip (unfoldr halved n) (iterate doubled m)
(swap $ quotRem h 2)
| otherwise = Nothing
in ( let x = foldr addedWhereOdd 0 pairs
in trace
v = foldr addedWhereOdd 0 pairs
( showDoubles pairs
addedWhereOdd (d, x) a
<> " = "
| 0 < d = (+) a x
<> show x
| otherwise = a
<> "\n"
)
x
)


---------------------- TRACE DISPLAY ---------------------
---------------------- TRACE DISPLAY ---------------------

showHalf :: Int -> String
showHalf :: Int -> String
showHalf x = "halve: " <> rjust 6 ' ' (show (quotRem x 2))
showHalf x = "halve: " <> rjust 6 ' ' (show (quotRem x 2))
Line 2,155: Line 2,151:
showDoubles xs =
showDoubles xs =
"double:\n"
"double:\n"
<> unlines
<> unlines (go <$> xs)
( fmap
<> intercalate " + " (xs >>= f)
where
( \x ->
bool
go x
(rjust 6 ' ' $ show $ snd x)
| 0 < fst x = "-> " <> rjust 3 ' ' (show $ snd x)
("-> " <> rjust 3 ' ' (show $ snd x))
| otherwise = rjust 6 ' ' $ show $ snd x
(0 < fst x)
f (r, q)
)
| 0 < r = [show q]
xs
| otherwise = []
)
<> intercalate
" + "
(xs >>= (\(r, q) -> bool [] [show q] (0 < r)))


rjust :: Int -> Char -> String -> String
rjust :: Int -> Char -> String -> String
rjust n c s = drop (length s) (replicate n c <> s)
rjust n c s = drop (length s) (replicate n c <> s)



--------------------------- TEST -------------------------
--------------------------- TEST -------------------------