Skip to content

Instantly share code, notes, and snippets.

@eldesh
Created February 20, 2012 03:38
Show Gist options
  • Save eldesh/1867638 to your computer and use it in GitHub Desktop.
Save eldesh/1867638 to your computer and use it in GitHub Desktop.
simple caesar cipher module implemented with SML
(* 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