Haskellで「Cipher」という独自のデータ型を作ろうとしています。26個あることに気がつきました!タイプが取ることができる値 (一度だけ使用されるアルファベットの文字の任意の組み合わせ)。
私は次のように始めました:
データ暗号 = ['a'..'z'] |
Haskellが組み合わせを「推測」できることは知っていますが、上記のように型が値のいずれかを取ることができるようにするにはどうすればよいですか?
Haskellで「Cipher」という独自のデータ型を作ろうとしています。26個あることに気がつきました!タイプが取ることができる値 (一度だけ使用されるアルファベットの文字の任意の組み合わせ)。
私は次のように始めました:
データ暗号 = ['a'..'z'] |
Haskellが組み合わせを「推測」できることは知っていますが、上記のように型が値のいずれかを取ることができるようにするにはどうすればよいですか?
簡単な答えはおそらく
import Data.Char (ord)
import Data.List (permutations)
newtype Cipher = Cipher String
ciphers = map Cipher . permutations $ ['a' .. 'z']
-- Lookup a characters value in the cipher
mapChar :: Cipher -> Char -> Char
mapChar ciph c = ciph !! ord c - ord 'a'
encode :: Cipher -> String -> String
encode ciph = map (mapChar ciph)
decode :: Cipher -> String -> String
decode -- Ill let you figure this out