Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Iainmon/f49e3524f751ecafb242925418db9fcb to your computer and use it in GitHub Desktop.

Select an option

Save Iainmon/f49e3524f751ecafb242925418db9fcb to your computer and use it in GitHub Desktop.
data List a = Cons a (List a) | Nil
listA = Cons 0 (Cons 2 (Cons 4 Nil))
listB = Cons 1 (Cons 0 (Cons 1 Nil))
tentothe :: Double -> Double
tentothe m = 10 ** m
reduce :: List Double -> Double -> Double
reduce (Cons n Nil) m = n * (tentothe m)
reduce (Cons n l) m = (reduce l (m + 1)) + (n * (tentothe m))
construct :: Double -> Double -> List Double
construct n m = Cons (extract n m) (if swallowed n (m + 1) then Nil else construct n (m + 1))
extract :: Double -> Double -> Double
extract n m = dfloor (n/tentothe m) - (dfloor (n/tentothe (m+1)) * 10)
dfloor :: Double -> Double
dfloor = fromIntegral . floor
-- extract' :: Double -> Double -> Double
-- extract' n m = dfloor (n/tentothe m) - (dfloor (n/tentothe (m+1)) * 10)
swallowed :: Double -> Double -> Bool
swallowed n m = (n / tentothe m) < 1
sumLists a b = construct ((reduce a 0) + (reduce b 0)) 0
listToArray :: List a -> [a]
listToArray (Cons e Nil) = [e]
listToArray (Cons e l) = e : (listToArray l)
main :: IO ()
-- main = print $ extract 1234 0
main = print $ listToArray $ sumLists listA listB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment