Created
May 27, 2020 08:22
-
-
Save Tritlo/e96f4e39ab2a09cbc99ad329afd00a63 to your computer and use it in GitHub Desktop.
I got nerd sniped to write a function that takes an n, and prints all strings of length n containing only 'X' and 'Y'
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 Data.Bits | |
printXY n = mapM_ print (gen n) | |
where gen 0 = [""] | |
gen n = map (f n) [0..2^(n-1)] | |
f :: Int -> Integer -> String | |
f n i = map (toXY . testBit i) [0..(n-1)] | |
toXY c = if c then 'X' else 'Y' | |
genRec n = (map ('X':) smaller) ++ (map ('Y':) smaller) | |
where smaller = genRec (n-1) | |
printXYRec n = mapM_ print (genRec n) | |
where | |
genRec 0 = [""] | |
genRec n = (map ('X':) smaller) ++ (map ('Y':) smaller) | |
where smaller = genRec (n-1) | |
printXYM n = mapM_ . print . replicateM n "XY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment