Created
February 5, 2013 00:59
-
-
Save BinRoot/4711213 to your computer and use it in GitHub Desktop.
Caesar Cipher Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- this is the original cipher function you will run | |
| cipher [] _ = [] | |
| cipher a b = rot (head a) b : cipher (tail a) b | |
| -- Take a look at this 'rotate' function. | |
| -- Rotate will convert a Char into another Char. | |
| -- This almost works. But it doesn't loop around z-->a. | |
| rotate :: Char -> Int -> Char | |
| rotate c n = if n==0 | |
| then c | |
| else rotate (succ c) (n-1) | |
| -- This rotate code works much better! | |
| -- It loops around. y -> z -> a -> b -> ... | |
| -- Try calling this: rot 'z' 2 | |
| rot :: Char -> Int -> Char | |
| rot c 0 = c | |
| rot 'z' n = rot 'a' (n-1) | |
| rot c n = rot (succ c) (n-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment