Skip to content

Instantly share code, notes, and snippets.

@abuseofnotation
Created April 2, 2025 12:36
Show Gist options
  • Save abuseofnotation/e0a31f9e92e0090bb71c9be472e61d9e to your computer and use it in GitHub Desktop.
Save abuseofnotation/e0a31f9e92e0090bb71c9be472e61d9e to your computer and use it in GitHub Desktop.
another Haskell task
-- Return a list of all combinations (i.e. order doesn't matter) of the given length.
-- Example: Given "abc" and 2 the answer is ["ab","ac","bc"] but order doesn't matter at either level.
combinations :: [a] -> Int -> [[a]]
combinations letters 0 = [[]]
combinations [] p = []
combinations (letter : letters) n = combinations letters n ++ map (letter :) (combinations letters (n -1))
main = print $ combinations "abc" 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment