Skip to content

Instantly share code, notes, and snippets.

@Rafailong
Created July 2, 2021 20:09
Show Gist options
  • Save Rafailong/dd072c673f1f95d1a23eca21cc91c2e8 to your computer and use it in GitHub Desktop.
Save Rafailong/dd072c673f1f95d1a23eca21cc91c2e8 to your computer and use it in GitHub Desktop.
(*
Atbash Cipher
The Atbash Cipher is simple: replace every letter with its “mirror” in the alphabet.
A is replaced by Z. B is replaced by Y. Etc. Write a function to calculate it.
Examples
(atbash "") ;=> ""
(atbash "hello") ;=> "svool"
(atbash "Clojure") ;=> "Xolqfiv"
(atbash "Yo!") ;=> "Bl!"
Please maintain capitalization and non-alphabetic characters.
*)
let pairWithMirror l = List.rev l |> List.zip l
let lowersWithMirror = pairWithMirror [ 'a' .. 'z' ]
let finder chr =
let predicate =
fun (o, _) -> o = (System.Char.ToLower chr)
List.tryFind predicate lowersWithMirror
|> Option.map snd
let folder str chr =
let mirror =
match (finder chr) with
| Some m when System.Char.IsUpper chr -> System.Char.ToUpper m
| Some m -> m
| _ -> chr
sprintf "%s%c" str mirror
let atbash (str: string) = Seq.fold folder System.String.Empty str
atbash "" = ""
&& atbash "hello" = "svool"
&& atbash "Clojure" = "Xolqfiv"
&& atbash "Yo!" = "Bl!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment