Skip to content

Instantly share code, notes, and snippets.

@L8D
Forked from dydx/weird-adder.hs
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save L8D/8980578 to your computer and use it in GitHub Desktop.
Save L8D/8980578 to your computer and use it in GitHub Desktop.
-- weird-adder.hs - Josh Sandlin - 6:45PM - 2/14/2010
-- wierd-adder.hs - Tenor Biel - 12:09PM - 2/13/201
-- Really weird way to add a range of numbers
-- takes an upper limit and returns a list of pairs
pairedRange :: Int -> [(Int, Int)]
pairedRange x = zip (filter odd [1..x]) (filter even [1..x])
-- adds a list of pairs
addPairs :: Num a => [(a, a)] -> [a]
addPairs = map (uncurry (+))
-- takes a number, builds a range, adds the pairs, then sums the list
sumPairedRange :: Int -> Int
sumPairedRange = sum . addPairs . pairedRange
sum' :: Int -> Int
sum' = sumPairedRange
-- sum [1..20] => 210
-- sum' 20 => 210 <- 20 is the upper limit of the range
@dydx
Copy link

dydx commented Jan 23, 2015

Interesting. I didn't know about the dot notation back when I was messing with this.

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