Created
October 29, 2019 23:58
-
-
Save morteako/328d27e762d4eb437623a12088a69a28 to your computer and use it in GitHub Desktop.
Simple way to get all possible merges of two lists in 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
allMerges :: [a] -> [a] -> [[a]] | |
allMerges [] ys = [ys] | |
allMerges xs [] = [xs] | |
allMerges left@(x:xs) right@(y:ys) = | |
map (x:) (allMerges xs right) ++ map (y:) (allMerges left ys) | |
-- > f [1,2,3] [11,12,13] | |
-- [ [1,2,3,11,12,13], | |
-- [1,2,11,3,12,13], | |
-- [1,2,11,12,3,13], | |
-- [1,2,11,12,13,3], | |
-- [1,11,2,3,12,13], | |
-- [1,11,2,12,3,13], | |
-- [1,11,2,12,13,3], | |
-- [1,11,12,2,3,13], | |
-- [1,11,12,2,13,3], | |
-- [1,11,12,13,2,3], | |
-- [11,1,2,3,12,13], | |
-- [11,1,2,12,3,13], | |
-- [11,1,2,12,13,3], | |
-- [11,1,12,2,3,13], | |
-- [11,1,12,2,13,3], | |
-- [11,1,12,13,2,3], | |
-- [11,12,1,2,3,13], | |
-- [11,12,1,2,13,3], | |
-- [11,12,1,13,2,3], | |
-- [11,12,13,1,2,3] | |
-- ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment