Ethiopian multiplication: Difference between revisions

→‎{{header|Haskell}}: Replaced with an implementation much like the second, but which I believe to be a more literal translation of the task description.
(→‎{{header|Haskell}}: Replaced with an implementation much like the second, but which I believe to be a more literal translation of the task description.)
Line 633:
 
=={{header|Haskell}}==
<lang haskell>halve, xdouble :: Integral a => xa `div`-> 2a
double xhalve = x *(`div` 2 )
double = (2 *)
-- 'odd' is included in the Prelude. It could be defined by:
-- odd = (== 1) . (`mod` 2)
 
ethiopicmult :: Integral a => a -> a -> a
ethiopicmult xa yb = ethiopicmult'sum x$ ymap 0snd where$ filter (odd . fst) $ zip
(takeWhile (>= 1) $ iterate halve a)
ethiopicmult' 0 _ acc = acc
(iterate double b)</lang>
ethiopicmult' plier pliand acc
| even plier = ethiopicmult' (halve plier) (double pliand) acc
| otherwise = ethiopicmult' (halve plier) (double pliand) (acc + pliand)</lang>
 
Alternately:
<lang haskell>ethiopicmult :: Integral a => a -> a -> a
ethiopicmult x y = sum [pliand | (plier, pliand) <- rowsNonZero, odd plier]
where rowsNonZero = takeWhile ((/= 0) . fst) rows
rows = zip (iterate (`div` 2) x) (iterate (* 2) y)</lang>
 
'''Usage example''' from the interpreter
845

edits