連続するスペースを一つにする
let rec explode_string s = match s with "" -> [] | _ -> s.[0] :: explode_string (String.sub s 1 (String.length s - 1)) let implode_string cs = let s = String.create (List.length cs) in let _ = List.fold_left (fun index c -> begin String.set s index c; index+1 end) 0 cs in s let remove_spaces s = implode_string (snd (List.fold_right (fun c (skip, s)-> match c with ' ' when skip -> (skip, s) | ' ' when not skip -> (true, c::s) | _ -> (false, c::s)) (explode_string s) (false, [])))
あるいは,Strモジュールを使えば,
let remove_spaces s = Str.global_replace (Str.regexp " +") " " s
とか.
implode_stringとexplode_stringはパーサとか書くときに便利なので定義しておくと良いと思います.SMLは標準ライブラリにあるけど,OCamlにはないので不便.
どうせstringはmutableなのでString.iterを使えってことなんでしょうかね.