Anonymous user
Execute a Markov algorithm: Difference between revisions
→{{header|Racket}}: Decomposed the main Markov algorithm and cleaned up the code.
(→{{header|Racket}}: Decomposed the main Markov algorithm and cleaned up the code.) |
|||
Line 2,053:
===The Markov algorithm interpreter===
The <tt>Markov-algorithm</tt>
<lang scheme>
Line 2,061:
(struct ->. (A B))
(let/cc stop ▼
; rewriting rules
(define (rewrite rule str)
; the cycle through rewriting rules
(define (apply-rules s) (foldl rewrite s rules))
; the result is a fixed point of rewriting procedure
(fixed-point apply-rules initial-string)))
;; replaces the first substring A to B in a string s
(define (replace A s B)
(and (regexp-match? (regexp-quote A) s)
(regexp-replace (regexp-quote A) s B)))
;; Finds the least fixed-point of a function
▲(define ((Markov-algorithm . rules) start)
▲ (let/cc stop
(let loop (
(if (equal? x fx) fx (
▲ (match r
▲ [(-> a b) (cond [(replace a res b) => new-cycle]
▲ [else res])]
▲ [(->. a b) (cond [(replace a res b) => stop]
▲ [else res])])))))
▲(define ((fixed-point f) x)
</lang>
|