Higher-Order Perl : Transforming Programs with Programs by Mark-Jason Dominus

Higher-Order Perl : Transforming Programs with Programs by Mark-Jason Dominus

Author:Mark-Jason Dominus
Language: eng
Format: epub
Tags: Perl, Programming
ISBN: 1-55860-701-3
Publisher: Morgan Kaufmann
Published: 2005-08-14T16:00:00+00:00


(s|stail).(t | ttail) = s.t | s.ttail |stail.t|stail.ttail

The first of these contains only one string. The middle two are simple transformations of the tails of S and T. The last one is a recursive call to the concat() function itself. So the code is simple:

sub concat { my ($S, $T) = @_; return unless $S && $T; my ($s, $t) = (head($S), head($T)); node("$s$t", promise { union( postcat(tail($S), $t), precat(tail($T), $s), concat(tail($S), tail($T)), ) }); }

precat() and postcat() are simple utility functions that concatenate a string to the beginning or end of every element of a stream:

sub precat { my ($s, $c) = @_; transform {"$c$_[0]"} $s; } sub postcat { my ($s, $c) = @_; transform {"$_[0]$c"} $s; }

An example:

# I’m /ˆ(a|b)(c|d)$/ my $z = concat( union(literal("a"), literal("b")), union(literal("c"), literal("d")), ); show($z); acbc adbd

The behavior of concat() is illustrated in Figure 6.3.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.