Amb: Difference between revisions

1,822 bytes added ,  15 years ago
(→‎SETL: Improved backtracking version and moved it in front)
Line 94:
</pre>
 
=={{header|ALGOL 68}}==
<!-- {{does not work with|ALGOL 68|Standard - with prelude inserted manually}} -->
<!-- {{does not work with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} -->
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
Note: This program violates ALGOL 68's scoping rules when a locally scoped
procedure is returned to a more global scope. [[ELLA ALGOL 68RS]] misses
this violation, but [[ALGOL 68 Genie]] spots it at run time and then produces
an assert. However [[ELLA ALGOL 68RS]] does produce the desired result,
but may potentially suffer from "mysterious" stack problems.
<lang algol>MODE STRINGS = [0][0]CHAR;
MODE YIELDSTRINGS = PROC(STRINGS)VOID;
MODE ITERSTRINGS = PROC(YIELDSTRINGS)VOID;
 
OP INITITERSTRINGS = (STRINGS self)ITERSTRINGS:
(YIELDSTRINGS yield)VOID: # scope violation #
FOR i TO UPB self DO
yield(self[i])
OD;
OP + = (ITERSTRINGS for strings, STRINGS b)ITERSTRINGS:
(YIELDSTRINGS yield)VOID: # scope violation #
for strings((STRINGS amb)VOID:(
[UPB amb + 1]STRING joined;
joined[:UPB amb] := amb;
STRING last string := amb[UPB amb];
CHAR last char := last string[UPB last string];
FOR i TO UPB b DO
IF last char = b[i][1] THEN
joined[UPB joined] := b[i];
yield(joined)
FI
OD
));
 
OP + = (STRINGS a, STRINGS b)ITERSTRINGS: INITITERSTRINGS a + b;
 
ITERSTRINGS for amb :=
STRINGS("the", "that", "a") +
STRINGS("frog", "elephant", "thing") +
STRINGS("walked", "treaded", "grows") +
STRINGS("slowly", "quickly");
 
STRINGS sep;
#FOR amv IN for amb DO#
for amb((STRINGS amb)VOID:(
print((amb[1]," ",amb[2]," ",amb[3]," ",amb[4], new line))
))
#OD#</lang>
Output:
<pre>
that thing grows slowly
</pre>
=={{Header|C}}==
Note: This uses the continuations code from http://homepage.mac.com/sigfpe/Computing/continuations.html