Skip to content

Instantly share code, notes, and snippets.

@tautologico
Last active August 29, 2015 14:10
Show Gist options
  • Save tautologico/c2d402d68d43f82ca56c to your computer and use it in GitHub Desktop.
Save tautologico/c2d402d68d43f82ca56c to your computer and use it in GitHub Desktop.
Finding the number of different letters in a string, ignoring spaces
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