(* Copyright 2007 Ken Friis Larsen *) let codes = [("A",".-"); ("B","-..."); ("C","-.-."); ("D","-.."); ("E","."); ("F","..-."); ("G","--."); ("H","...."); ("I",".."); ("J",".---"); ("K","-.-"); ("L",".-.."); ("M","--"); ("N","-."); ("O","---"); ("P",".--."); ("Q","--.-"); ("R",".-."); ("S","..."); ("T","-"); ("U","..-"); ("V","...-"); ("W",".--"); ("X","-..-");("Y","-.--"); ("Z","--..")] let rec decode (input : string) = if input = "" then { -> input } else { for c, code in codes when input.StartsWith code for rest in decode(input.Substring(code.Length)) -> c + rest } let test s code = if Seq.exists (fun x -> x = s) (decode code) then printf "%s can be decoded as %s\n" code s else printf "%s can NOT be decoded as %s\n" code s let _ = let start = System.DateTime.Now in let res = Seq.length(decode "...---..-....-...----") in printf "%f\n" ((System.DateTime.Now - start).TotalSeconds);; (* let _ = test "SOFIASOF" "...---..-....-...---..-.";; let _ = test "SOPHIA" "...---..-....-";; let _ = test "EUGENIA" "...---..-....-";; *)