Skip to content

Instantly share code, notes, and snippets.

@sasha1sum
Last active December 15, 2015 22:39
Show Gist options
  • Save sasha1sum/5334462 to your computer and use it in GitHub Desktop.
Save sasha1sum/5334462 to your computer and use it in GitHub Desktop.
foldleach and comb
module Main where
-- fold an operation down a list of lists
foldleach :: (a -> b -> a) -> a -> [[b]] -> [a]
foldleach f b xs = map (foldl f b) xs
foldl1each :: (a -> a -> a) -> [[a]] -> [a]
foldl1each f xs = map (foldl1 f) xs
-- find combinations from list
comb :: Int -> [a] -> [[a]]
comb n [] = []
comb 1 xs = map (:[]) xs
comb n (x:xs) = aa ++ bb where
aa = map (x:) $ comb (n-1) xs
bb = comb n xs
-- usage:
-- foldl1each (*) (comb 2 [1,2,3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment