Execute a Markov algorithm: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output and ruleset 6; use a right arrow string instead of equivalence character.
(→‎{{header|Kotlin}}: Changed 'do' loop to avoid use of label + removed surplus whitespace)
m (→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output and ruleset 6; use a right arrow string instead of equivalence character.)
Line 3,443:
Code was added to the REXX example to optionally list the contents of the ruleset and/or the Markov entries.
<br>Also, blank lines in the ruleset were treated as comments.
<lang rexx>/*REXX program executes a Markov algorithm(s) against specified entries. */
parse arg low high . /*allows which ruleset to process. */
if low=='' | low=='",'" then low=1 /*Not specified? Then use the default.*/
if high=='' | high=='",'" then high=6 /* " " " " " " */
tellE= low<0; tellR= high<0 /*flags: used to display file contents.*/
call readEntry
do j=abs(low) to abs(high) /*process each of these rulesets. */
call readRules j /*read a particular ruleset. */
call execRules j /*execute " " " */
say 'result for ruleset' j "───►" !.j
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
execRules: parse arg q .; if tellE | tellR then say /*show a blank line?*/
do f=1
do k=1 while @.k\==''; if left(@.k, 1)=='#' | @.k='' then iterate
parse var @.k a ' ->' b; /*obtain the A & B a=strip(a); parts from b=strip(b)rule.*/
fullstop= left(b,1)=='.' /*isa=strip(a); this a "fullstop" rule?b=strip(b) /*strip leading and/or trailing blanks.*/
if fullstop then b fullstop=substr left(b,2 1)==. /*purify the B part of the rule. /*is this a "fullstop" rule? 1≡yes */
old=!.q if fullstop then b=substr(b, 2) /*purify the B part of the rule. /*remember the value before the change.*/
!.q=changestr(a, old=!.q, b) /*implement the ruleset change. /*remember the value before the change.*/
if fullstop then if old\= !.q=changestr(a, !.q, b) then return /*shouldimplement the ruleset change. we stop? */
if fullstop then if old\==!.q then iteratereturn f /*Has Entry changed? Then start/*should we stop? over.*/
if old\==!.q then iterate f /*Has Entry changed? Then start over.*/
end /*k*/
leave end /*k*/
end /*f*/ return
end /*kf*/
return
return
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
readRules: parse arg ? .; rFID='MARKOV_R.'?; if tellR then say
@.readEntry: eFID= 'MARKOV.ENT'; if tellE then say /*placeholdershow fora allblank the Markov rules.line?*/
do r !.=1 while lines(rFID)\==0 /*readplaceholder thefor inputall filethe untiltest End-Of-Fileentries.*/
do e=1 while lines(eFID)\==0 /*read the input file until End-Of-File*/
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4)'≡'@.r
end /*r*/ !.e=linein(eFID); if tellE then say 'test entry' e /*(above) read and"───►" maybe echo the rule!.*/e
!.= end /*e*/ /*placeholder for[↑] all theread testand entriesmaybe echo the entry. */
return
return
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
readEntry: eFID='MARKOV.ENT'; if tellE then say
readRules: parse arg ? .; rFID= 'MARKOV_R.'?; if tellR then say /*show a blank line?*/
!.= /*placeholder for all the test entries.*/
do e @.=1 while lines(eFID)\==0 /*readplaceholder thefor inputall filethe untilMarkov End-Of-Filerules.*/
do r=1 while lines(rFID)\==0 /*read the input file until End-Of-File*/
!.e=linein(eFID); if tellE then say 'test entry' e"≡"!.e
end /*e*/ @.r=linein(rFID); if tellR then say 'ruleSet' /*?"."left(abover,4) read and maybe echo the'───►' entry*/@.r
end /*r*/ /* [↑] read and maybe echo the rule. */
return</lang>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]].
<br><br>
'''{{out|output''' |text=&nbsp; when using the default input and files:}}
<pre>
result for ruleset 1≡I1 ───► I bought a bagB of applesAs from myT brotherS.
result for ruleset 2≡I2 ───► I bought a bag of apples from T shop.
result for ruleset 3≡I3 ───► I bought a bag of apples with my money from T shop.
result for ruleset 4≡111111111111111111114 ───► 11111111111111111111
result for ruleset 5≡00011H11110005 ───► 00011H1111000
result for ruleset 6≡6 ───► |||||
</pre>
'''{{out|Ruleset &nbsp;6'''|text=&nbsp; is:}}
<pre>
# Rewrite binary numbers to their unary value (| bars).