Skip to content

Instantly share code, notes, and snippets.

@dermusikman
Created August 8, 2015 06:58
Show Gist options
  • Save dermusikman/f7df5030828dae5d4a3d to your computer and use it in GitHub Desktop.
Save dermusikman/f7df5030828dae5d4a3d to your computer and use it in GitHub Desktop.
(╯°□°)╯︵ ןןǝʞsɐɥ
me@le-me $ cat scratch.hs
evenSum :: Integral a => [a] -> a
evenSum l = accumSum 0 l
where
accumSum n [] = n
accumSum n (x:xs) =
if even x
then accumSum (n+x) xs
else accumSum x xs
evenSum' :: Integral a => [a] -> a
evenSum' l = accumSum 0 l
where
accumSum n [] = n
accumSum n (x:xs) =
if even x
then accumSum (n+x) xs
else accumSum n xs
main = do
print $ evenSum [1..5]
print $ evenSum' [1..5]
me@le-me $ ./scratch
5
6
@dermusikman
Copy link
Author

-- "The only winning move is not to play." -Joshua
import Data.List
evenSum''' :: Integral a => [a] -> a
evenSum''' l = foldl' (+) 0 (filter even l)
-- haskell ノ( º _ ºノ)

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