My First -not so- Non-trivial Haskell Program

To write a caeser cipher given a string (all in lower case) and the shift count:

rotateLetter :: Char -> Int -> Char
rotateLetter a 0 = a
rotateLetter 'z' n = rotateLetter 'a' (n-1)
rotateLetter a n = rotateLetter (succ a) (n - 1)

caeserCipher :: [Char] -> Int -> [Char]
caeserCipher "" n = ""
caeserCipher (x:xs) n = (rotateLetter x n) : caeserCipher xs n
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s