Created
April 30, 2021 22:09
-
-
Save fakuivan/addee1b40099a97c749515869b1470f3 to your computer and use it in GitHub Desktop.
Algorithm implemented in Haskell for generating Grey codes
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
-- generalized n-ary gray code generator | |
gray :: (Integral a) => [b] -> a -> [[b]] | |
gray elems 1 = map (:[]) elems | |
gray elems n = | |
concat $ zipWith ($) prependers $ flipN $ gray elems (n-1) | |
where flipN = (take $ length elems) . (iterate reverse) | |
prependers = [ map (elem:) | elem <- elems ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment