Background
I am a total beginner in Haskell, so after reading the Starting out-chapter of "Learn you a Haskell", I wanted to create my first program that actually does something.
I decided to do the famous Caesar-Cipher.
Code
import Data.Char
encryptChar :: Char -> Int -> Int
encryptChar char shift = if ord char > 64 && ord char < 91 --Only encrypt A...Z
then (if ord char + shift > 90 --"Z" with shift 3 becomes "C" and not "]"
then ord char + shift - 26
else ord char + shift)
else ord char
decryptChar :: Char -> Int -> Int
decryptChar char shift = if ord char > 64 && ord char < 91
then (if ord char - shift < 65
then ord char - shift + 26
else ord char - shift)
else ord char
encrypt :: String -> Int -> String
encrypt string shift = [chr (encryptChar (toUpper x) shift) | x <- string] --"Loop" through string to encrypt char by char
decrypt :: String -> Int -> String
decrypt string shift = [chr (decryptChar (toUpper x) shift) | x <- string]
main = print(decrypt "KHOOR, ZRUOG!" 3)
(The code does work as intended.)
Question(s)
- How can this code be improved in general?
- Do I follow the style guide of Haskell (indentation, etc.)?
- I have a background in imperative languages. Did I do something that is untypical for functional programming languages?
I would appreciate any suggestions.