Download code

Jump to: navigation, search

Back to Markov_algorithm_simulator_(Ocaml)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

markov.ml

 1 (* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Markov_algorithm_simulator_(Ocaml)?oldid=13670
14 *)
15 
16 #load "str.cma";;
17 open Str
18 type word = string
19 type rule = word * word * bool
20 type algor = rule list
21 let successor = ["aL", "La", false;
22                  "a0", "0a", false;
23                  "a" , "b" , false;
24                  "Lb", "b0", false;
25                  "0b", "L" , true ;
26                  "b" , "L" , true ;
27                  ""  , "a" , false]
28 let rec contains (s:string) (sub:string) : bool =
29   try
30     ignore(search_forward (regexp_string sub) s 0);
31     true
32   with Not_found -> false
33 let find_rule (a:algor) (w:word) : rule =
34   List.find (fun (l,_,_) -> contains w l) a
35 let apply_rule (l,r,_:rule) (s:word) : word =
36   replace_first (regexp_string l) r s
37 let apply_alg (a:algor) (w:word) : word * bool =
38   let _,_,b as r = find_rule a w in
39   apply_rule r w, b
40 let rec run (a:algor) (w:word) : word list =
41   try let w', flg = apply_alg a w in
42     if not flg then w :: run a w' (* Normal rule was applied *)
43     else            [w; w']       (* Halting rule was applied *)
44   with Not_found -> [w]           (* No rule was applied *)
45 


hijacker
hijacker
hijacker
hijacker