Ethiopian multiplication: Difference between revisions
→{{header|Haskell}}: Fixed.
(→{{header|OCaml}}: incorrect. Program needs to define three functions to halve double and test odd/even as stated in the task description.) |
Underscore (talk | contribs) (→{{header|Haskell}}: Fixed.) |
||
Line 633:
=={{header|Haskell}}==
<lang haskell>
halve, double :: Integral a => a -> a
halve = (`div` 2)
double = (2 *)
odd = (== 1) . (`mod` 2)▼
odd :: Integral a => a -> Bool
▲odd = (== 1) . (`mod` 2)
ethiopicmult :: Integral a => a -> a -> a
ethiopicmult a b = sum $ map snd $ filter (odd . fst) $ zip
(takeWhile (>= 1) $ iterate halve a)
(iterate double b)
main = print $ ethiopicmult 17 34 == 17 * 34</lang>
'''Usage example''' from the interpreter
|