Skip to content

Instantly share code, notes, and snippets.

@L8D

L8D/weird-adder.hs

Forked from dydx/weird-adder.hs
Last active Aug 29, 2015
Embed
What would you like to do?
-- 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

This comment has been minimized.

Copy link

@dydx 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
You can’t perform that action at this time.