Created
February 20, 2012 03:38
-
-
Save eldesh/1867638 to your computer and use it in GitHub Desktop.
simple caesar cipher module implemented with SML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* see. http://d.hatena.ne.jp/a-hisame/20120217/1329499672 *) | |
(* =========================================================== | |
* for using | |
* > sml | |
* > use "caesar.sml"; | |
* > Caesar.encode "hoge"; | |
* val it = "mtlj" : string | |
* > _ | |
========================================================== *) | |
structure Caesar : | |
sig | |
val encode : int -> string -> string | |
val decode : int -> string -> string | |
end = | |
struct | |
fun let2int c = ord c - ord #"a" | |
fun int2let n = chr (ord #"a" + n) | |
fun shift n c = if Char.isLower c | |
then int2let((let2int c + n) mod 26) | |
else c | |
(* assert (encode 1 "hal") = "ibm" *) | |
fun encode n = implode o map (shift n) o explode | |
fun decode n = encode (~n) | |
end (* structure Caesar *) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment