Task

Explicitly implement   long multiplication.

This is one possible approach to arbitrary-precision integer algebra.



For output, display the result of    264 * 264.

Optionally, verify your result against builtin arbitrary precision support.

The decimal representation of    264    is:

18,446,744,073,709,551,616

The output of    264 * 264    is    2128,    and is:

340,282,366,920,938,463,463,374,607,431,768,211,456



import Data.List (transpose, inits)
import Data.Char (digitToInt)
 
longmult :: Integer -> Integer -> Integer
longmult x y = foldl1 ((+) . (10 *)) (polymul (digits x) (digits y))
 
polymul :: [Integer] -> [Integer] -> [Integer]
polymul xs ys =
  sum <$>
  transpose
    (zipWith
       (<>)
       (inits $ repeat 0)
       ((\f x -> fmap ((<$> x) . f)) (*) xs ys))
 
digits :: Integer -> [Integer]
digits = fmap (fromIntegral . digitToInt) . show
 
main :: IO ()
main = print $ (2 ^ 64) `longmult` (2 ^ 64)