ROT13 in Haskell


I put this together for my Intro to Functional Programming class, when we were given the task of creating a ROT13 function which works on alphanumeric characters and leaves other characters alone. After completing my own version, I did a brief search on Google for other solutions, and several of them, notably the first few results, were quite poor alternatives.

module Rot13 where import Char lSet = ['a'..'m'] ++ ['A'..'M'] ++ ['0'..'4'] rSet = ['n'..'z'] ++ ['N'..'Z'] ++ ['5'..'9'] charMap = (zip lSet rSet) ++ (zip rSet lSet) findInSwitch [] myChar = myChar findInSwitch (c:cs) myChar = if (fst c) == myChar then (snd c) else (findInSwitch cs myChar) rot13 string = map (findInSwitch charMap) string

The basic methodology here is to create a list of tuples of all characters and their encrypted counterpart. Then to iterate through the list for each character and finding its counterpart. If the character is not found (meaning it's not alphanumeric) then the original character is returned.

Read more: Literate Programs