Look-and-say sequence: Difference between revisions

(another ocaml solution using pcre)
Line 684:
(next))
(string_of_int 1) 10</lang>
 
Alternately, using the included Str library:
 
<lang ocaml>#load "str.cma";;
 
let lookandsay =
Str.global_substitute (Str.regexp "\\(.\\)\\1*")
(fun s -> let m = Str.matched_string s in
string_of_int (String.length m) ^
String.sub m 0 1)
 
let () =
let num = ref "1" in
print_endline !num;
for i = 1 to 10 do
num := lookandsay !num;
print_endline !num;
done</lang>
 
Another solution using the pcre bindings:
Anonymous user