Skip to content

Instantly share code, notes, and snippets.

@BinRoot
Created February 5, 2013 00:59
Show Gist options
  • Select an option

  • Save BinRoot/4711213 to your computer and use it in GitHub Desktop.

Select an option

Save BinRoot/4711213 to your computer and use it in GitHub Desktop.
Caesar Cipher Example
-- 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