Reputation: 891
I am trying to make my own data type called "Cipher" in Haskell. I have realised that there are 26! values the type can take (any combination of chars in the alphabet used once and only once).
I have started it like this:
data Cipher = ['a'..'z'] |
I know Haskell can "guess" combinations, but how can I tell it I want the type to be able to take any of the values as stated above?
Upvotes: 0
Views: 344
Reputation: 53901
A simple answer might be
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
Upvotes: 1