Last active
August 29, 2015 14:10
-
-
Save tautologico/c2d402d68d43f82ca56c to your computer and use it in GitHub Desktop.
Finding the number of different letters in a string, ignoring spaces
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
let string_fold s f ini = (* this is a left fold *) | |
let len = String.length s in | |
let rec loop i res = | |
if i >= len then res else loop (i+1) (f s.[i] res) | |
in | |
loop 0 ini | |
let explode s = (* a right string fold wouldn't need the reverse *) | |
List.rev @@ string_fold s (fun c l -> c :: l) [] | |
let unique l = | |
List.fold_right (fun i l -> if List.mem i l then l else i :: l) l [] | |
let letter_cover s = | |
explode s | |
|> List.filter (fun c -> c <> ' ') | |
|> unique | |
|> List.length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment