O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Our Model

 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required