Execute a Markov algorithm: Difference between revisions
Content added Content deleted
(Updated D entry) |
m (On seeing this again, the use of a module hinders clarity, so I removed it) |
||
Line 2,076: | Line 2,076: | ||
Str.replace_first a b s |
Str.replace_first a b s |
||
⚫ | |||
(* At their simplest, modules can function like namespaces... though they're much more. *) |
|||
⚫ | |||
module Markov = struct |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
let s = input_line cin in |
|||
⚫ | |||
⚫ | |||
⚫ | |||
else if string_match rule s 0 then |
|||
let a = regexp_string (matched_group 1 s) in |
|||
⚫ | |||
else if string_match rule s 0 then |
|||
parse_line ((a,b,terminate)::rules) |
|||
let a = regexp_string (matched_group 1 s) in |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
let rec run rules text = |
|||
let rec apply s = function |
|||
| [] -> s |
|||
| (a,b,term)::next -> |
|||
try |
|||
let s' = subst a b s in |
|||
if term then s' else run rules s' |
|||
with Not_found -> apply s next |
|||
in apply text rules |
|||
end |
|||
let _ = |
let _ = |
||
Line 2,110: | Line 2,107: | ||
print_endline "Expecting one argument: a filename where rules can be found." |
print_endline "Expecting one argument: a filename where rules can be found." |
||
else |
else |
||
let rules = try_finally (open_in Sys.argv.(1)) |
let rules = try_finally (open_in Sys.argv.(1)) parse_rules close_in in |
||
(* Translate lines read from stdin, until EOF *) |
(* Translate lines read from stdin, until EOF *) |
||
let rec translate () = |
let rec translate () = |
||
print_endline ( |
print_endline (run rules (input_line stdin)); |
||
translate () |
translate () |
||
in try translate () with End_of_file -> ()</lang> |
in try translate () with End_of_file -> ()</lang> |