Last active
August 29, 2015 13:57
-
-
Save nickserv/9405859 to your computer and use it in GitHub Desktop.
Nested sums in Ruby and Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mapSum :: Num a => (a -> a) -> [a] -> a | |
mapSum f xs = sum $ map f xs | |
nestedSum :: Num a => [a] -> [a] -> (a -> a -> a) -> a | |
nestedSum xs ys f = mapSum (\ x -> mapSum (f x) ys) xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nested_sum(outer, inner) | |
outer.reduce(0) do |outer_sum, i| | |
outer_sum + inner.reduce(0) do |inner_sum, j| | |
inner_sum + yield(i, j) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment