July 2017
Intermediate to advanced
284 pages
6h 45m
English
| | caesar.hs |
| | import Data.Char |
| | import Data.Ix |
| | import Foreign.C |
| | import System.IO.Unsafe |
| | import Test.QuickCheck |
| | caesar :: Int -> String -> String |
| | caesar k = map f |
| | where |
| | f c |
| | | inRange ('A', 'Z') c = chr $ ord 'A' |
| | + (ord c - ord 'A' + k) `mod` 26 |
| | | otherwise = c |
In this example, we recreate the Caesar implementation. You can ignore the slew of import statements for now. They will all be used during this chapter, so we’re putting them in place so we don’t have to worry about them later.
Our Haskell implementation starts with a type signature. It accepts an Int and a String, and returns a String. The function maps over all the characters in the message and applies the shift as long as the character is in the range of uppercase ...
Read now
Unlock full access