Execute a Markov algorithm: Difference between revisions

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