Ethiopian multiplication: Difference between revisions

(→‎{{header|OCaml}}: incorrect. Program needs to define three functions to halve double and test odd/even as stated in the task description.)
Line 633:
 
=={{header|Haskell}}==
<lang haskell>halve,import doublePrelude ::hiding Integral a => a -> a(odd)
 
halve, double :: Integral a => a -> a
halve = (`div` 2)
double = (2 *)
-- 'odd' although included in the Prelude, it is defined here to meet the task specification as:
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)</lang>
 
main = print $ ethiopicmult 17 34 == 17 * 34</lang>
 
'''Usage example''' from the interpreter
845

edits