Amb: Difference between revisions
Content deleted Content added
m →version 1: split a one-line DO loop into two statements. |
Alextretyak (talk | contribs) Added 11l |
||
Line 46: | Line 46: | ||
The goal of this task isn't to simply process the four lists of words with explicit, deterministic program flow such as nested iteration, to trivially demonstrate the correct output. The goal is to implement the Amb operator, or a facsimile thereof that is possible within the language limitations. |
The goal of this task isn't to simply process the four lists of words with explicit, deterministic program flow such as nested iteration, to trivially demonstrate the correct output. The goal is to implement the Amb operator, or a facsimile thereof that is possible within the language limitations. |
||
=={{Header|11l}}== |
|||
{{trans|Nim}} |
|||
<lang 11l>F amb(comp, options, prev = ‘’) |
|||
I options.empty |
|||
R Array[String]() |
|||
L(opt) options[0] |
|||
// If this is the base call, prev is empty and we need to continue. |
|||
I prev != ‘’ & !comp(prev, opt) |
|||
L.continue |
|||
// Take care of the case where we have no options left. |
|||
I options.len == 1 |
|||
R [opt] |
|||
// Traverse into the tree. |
|||
A res = amb(comp, options[1..], opt) |
|||
// If it was a failure, try the next one. |
|||
if !res.empty |
|||
R opt [+] res // We have a match |
|||
R Array[String]() |
|||
A sets = [[‘the’, ‘that’, ‘a’], |
|||
[‘frog’, ‘elephant’, ‘thing’], |
|||
[‘walked’, ‘treaded’, ‘grows’], |
|||
[‘slowly’, ‘quickly’]] |
|||
A result = amb((s, t) -> s.last == t[0], sets) |
|||
print(result.join(‘ ’))</lang> |
|||
{{out}} |
|||
<pre>that thing grows slowly</pre> |
|||
=={{Header|Ada}}== |
=={{Header|Ada}}== |