{- |
Description: Gray codes
Copyright: Copyright (C) 2021 Yoo Chung
License: GPL-3.0-or-later
Maintainer: dev@chungyc.org

Some solutions to "Problems.P49" of Ninety-Nine Haskell "Problems".
-}
module Solutions.P49 (gray) where

-- | Returns the n-bit Gray code.
gray :: Int -> [String]
gray :: Int -> [String]
gray Int
0 = [String
""]
gray Int
1 = [String
"0", String
"1"]
gray Int
n
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1     = (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Char
'0':) [String]
g [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Char
'1':) ([String] -> [String]
forall a. [a] -> [a]
reverse [String]
g)
  | Bool
otherwise = [String]
forall a. HasCallStack => a
undefined
  where g :: [String]
g = Int -> [String]
gray (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)