RosettaCodeData/Task/Long-multiplication/Haskell/long-multiplication-1.hs

21 lines
508 B
Haskell
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
import Data.List (transpose, inits)
2013-06-05 21:47:54 +00:00
import Data.Char (digitToInt)
2019-09-12 10:33:56 -07:00
longmult :: Integer -> Integer -> Integer
longmult x y = foldl1 ((+) . (10 *)) (polymul (digits x) (digits y))
2013-04-10 21:29:02 -07:00
2017-09-23 10:01:46 +02:00
polymul :: [Integer] -> [Integer] -> [Integer]
2019-09-12 10:33:56 -07:00
polymul xs ys =
sum <$>
transpose
(zipWith
(++)
(inits $ repeat 0)
((\f x -> fmap $ flip fmap x . f) (*) xs ys))
2013-04-10 21:29:02 -07:00
2019-09-12 10:33:56 -07:00
digits :: Integer -> [Integer]
digits = fmap (fromIntegral . digitToInt) . show
2013-06-05 21:47:54 +00:00
2017-09-23 10:01:46 +02:00
main :: IO ()
2013-06-05 21:47:54 +00:00
main = print $ (2 ^ 64) `longmult` (2 ^ 64)