Skip to content

Instantly share code, notes, and snippets.

@dydx
Created February 15, 2010 00:29
Show Gist options
  • Save dydx/304353 to your computer and use it in GitHub Desktop.
Save dydx/304353 to your computer and use it in GitHub Desktop.
-- weird-adder.hs - Josh Sandlin - 6:45PM - 2/14/2010
-- 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 [ a | a <- [1..x], odd a ] [ b | b <- [1..x], even b ]
-- adds a list of pairs
addPairs :: (Num a) => [(a, a)] -> [a]
addPairs x = [ a + b | (a, b) <- x ]
-- takes a number, builds a range, adds the pairs, then sums the list
sumPairedRange :: Int -> Int
sumPairedRange x = sum (addPairs (pairedRange x))
sum' :: Int -> Int
sum' x = sumPairedRange x
-- sum [1..20] => 210
-- sum' 20 => 210 <- 20 is the upper limit of the range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment