Skip to content

Instantly share code, notes, and snippets.

@sboehler
Created December 1, 2017 17:52
Show Gist options
  • Save sboehler/4424efe045c1831e885136e5a29067bd to your computer and use it in GitHub Desktop.
Save sboehler/4424efe045c1831e885136e5a29067bd to your computer and use it in GitHub Desktop.
module Main where
import Data.Monoid(Sum(..), getSum)
import Data.Char(digitToInt)
import System.Environment(getArgs)
main :: IO ()
main = do
(arg:_) <- getArgs
let list = (Sum . digitToInt) <$> arg
print $ (getSum . mysum) list
mysum :: (Eq a, Monoid a) => [a] -> a
mysum l@(first:_) =
let
mysum' (x:y:rest') = (if x == y then x else mempty) `mappend` mysum' (y:rest')
mysum' [x] = if first == x then first else mempty
mysum' [] = mempty
in
mysum' l
mysum [] = mempty
@benedek-tw
Copy link

Now just define (:^+^:) = mappend and o = mempty and it almost looks like proper addition again! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment