Last active
April 17, 2020 17:38
-
-
Save crclark96/90761e1250a274f32fddc6f46138d1c3 to your computer and use it in GitHub Desktop.
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
import Control.Monad | |
generateParens :: Int -> [String] | |
generateParens n = generateParens' n "" | |
generateParens' :: Int -> String -> [String] | |
generateParens' n xs | |
| n == o = [xs ++ replicate u ')'] | |
| u == 0 = generateParens' n (xs ++ "(") | |
| otherwise = [xs ++ "(", xs ++ ")"] >>= generateParens' n | |
where o = length $ filter (=='(') xs -- open parens | |
u = o - (length $ filter (==')') xs) -- unmatched parens | |
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
*Main> generateParens 3 | |
["((()))","(()())","(())()","()(())","()()()"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment