-2

I am currently working in an exercise to make a caesar cipher, but for to finish that i need to find items in a table. It's a table which is offset to right by 3 elements.

There is my table:

table :: [(Char, Char)]
table = zip ['a'..'z'] (['d'..'z'] ++ ['a'..'c'])
[('a','d'),('b','e'),('c','f'),('d','g'),('e','h'),('f','i'),('g','j'),('h','k'),('i','l'),('j','m'),('k','n'),('l','o'),('m','p'),('n','q'),('o','r'),('p','s'),('q','t'),('r','u'),('s','v'),('t','w'),('u','x'),('v','y'),('w','z'),('x','a'),('y','b'),('z','c')]

And I need to find an alphabetic character to give back it's pair. Like:

shift :: [(Char, Char)] -> Char -> Char 
shift table 'z' ...

result:

shift table 'y'
'b'

I haven't find anything on books or on the internet, that's why i am asking. Thank you for your help!

Attila Fodor
  • 27
  • 2
  • 6
  • 1
    You are looking for `lookup :: Eq => a -> [(a, b)] -> Maybe b`. – chepner Oct 29 '18 at 15:55
  • If this is an exercise then fine but under normal conditions this is an inefficient way of constructing a table. Just use the `Data.Map` package. – Redu Oct 29 '18 at 17:07

1 Answers1

1

you cannot be certain that you'll find the value you're looking for in the table, something like this should work

find :: Eq a => a -> [(a,b)] -> Maybe b
find c [] = Nothing
find c ((k,v):xs) | c==k = Just v
                  | otherwise = find c xs
karakfa
  • 66,216
  • 7
  • 41
  • 56