Last active
April 16, 2019 10:46
-
-
Save MiloszKrajewski/4089fb1de3e83679b7e181432f271eb7 to your computer and use it in GitHub Desktop.
Function to create string "between" two other strings
This file contains hidden or 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
| open System | |
| let rec between a z = | |
| if a = z then a | |
| elif a > z then between z a | |
| else | |
| let rec between a z acc = | |
| match a, z with | |
| | [], z -> between ['a'] z acc | |
| | a, [] -> between a ['z'] acc | |
| | a0 :: a, z0 :: z -> | |
| match int z0 - int a0 with | |
| | 0 -> between a z (a0 :: acc) | |
| | 1 -> between a ['z'] (a0 :: acc) | |
| | d -> let m = char (int a0 + (d / 2)) in m :: acc | |
| between (a |> List.ofSeq) (z |> List.ofSeq) [] |> Array.ofList |> Array.rev |> String | |
| assert (between "a" "z" = "m") | |
| assert (between "a" "b" = "am") | |
| assert (between "z" "a" = between "a" "z") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment