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== |
if low=='' | low=="," then low=1 /*Not specified? Then use the default.*/ |
||
if high=='' | high== |
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" |
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 |
parse var @.k a ' ->' b /*obtain the A & B parts from rule.*/ |
||
a=strip(a); b=strip(b) /*strip leading and/or trailing blanks.*/ |
|||
fullstop= left(b, 1)==. /*is this a "fullstop" rule? 1≡yes */ |
|||
if fullstop then b=substr(b, 2) /*purify the B part of the rule. */ |
|||
old=!.q /*remember the value before the change.*/ |
|||
!.q=changestr(a, !.q, b) /*implement the ruleset change. */ |
|||
if old\==!.q then |
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*/ |
|||
return |
|||
⚫ | |||
return |
|||
return |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
readEntry: eFID= 'MARKOV.ENT'; if tellE 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*/ |
|||
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4)'≡'@.r |
|||
!.e=linein(eFID); if tellE then say 'test entry' e "───►" !.e |
|||
⚫ | |||
return |
|||
return |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
readEntry: eFID='MARKOV.ENT'; if tellE then say |
|||
⚫ | |||
⚫ | |||
@.= /*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 |
|||
@.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 '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. |
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. |
||
<br><br> |
<br><br> |
||
{{out|output|text= when using the default input and files:}} |
|||
<pre> |
<pre> |
||
result for ruleset |
result for ruleset 1 ───► I bought a B of As from T S. |
||
result for ruleset |
result for ruleset 2 ───► I bought a bag of apples from T shop. |
||
result for ruleset |
result for ruleset 3 ───► I bought a bag of apples with my money from T shop. |
||
result for ruleset |
result for ruleset 4 ───► 11111111111111111111 |
||
result for ruleset |
result for ruleset 5 ───► 00011H1111000 |
||
result for ruleset |
result for ruleset 6 ───► ||||| |
||
</pre> |
</pre> |
||
{{out|Ruleset 6|text= is:}} |
|||
<pre> |
<pre> |
||
# Rewrite binary numbers to their unary value (| bars). |
# Rewrite binary numbers to their unary value (| bars). |