Execute a Markov algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Fixed lang tag (whoops!).)
(Added Perl.)
Line 247: Line 247:
I bought a bag of apples from my brother.</lang>
I bought a bag of apples from my brother.</lang>
'''Discussion''': This solution implemented in 20 seconds and doesn't fully implement a Markov algorithm. More details on [[Talk:Markov Algorithm#J|the talk page]].
'''Discussion''': This solution implemented in 20 seconds and doesn't fully implement a Markov algorithm. More details on [[Talk:Markov Algorithm#J|the talk page]].

=={{header|Perl}}==
This program expects a source file as an argument and uses the standard input and output devices for the algorithm's I/O.

<lang perl>@ARGV == 1 or die "Please provide exactly one source file as an argument.\n";
open my $source, '<', $ARGV[0] or die qq[I couldn't open "$ARGV[0]" for reading. ($!.)\n];
my @rules;
while (<$source>)
{/\A#/ and next;
my @a = /(.*?)\s+->\s+(\.?)(.*)/ or die "Syntax error: $_";
push @rules, \@a;}
close $source;

my $input = do {local $/; <>;};

OUTER:
{foreach (@rules)
{my ($from, $terminating, $to) = @$_;
$input =~ s/\Q$from\E/$to/
and ($terminating ? last OUTER : redo OUTER);}}

print $input;</lang>


=={{header|Python}}==
=={{header|Python}}==