Amb: Difference between revisions
→{{header|langur}}
mNo edit summary |
Langurmonkey (talk | contribs) |
||
(16 intermediate revisions by 9 users not shown) | |||
Line 908:
Words 4: (slowly, quickly)
Solution: (that, thing, grows, slowly)
</pre>
===Without external libraries===
<syntaxhighlight lang="c++">
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
std::string join(const std::string& delimiter, const std::vector<std::string>& list) {
return list.empty() ? "" : std::accumulate(++list.begin(), list.end(), list[0],
[delimiter](auto& a, auto& b) { return a + delimiter + b; });
}
std::vector<std::string> amb(std::function<bool(std::string&, std::string&)> func,
std::vector<std::vector<std::string>> options, std::string previous) {
if ( options.empty() ) {
return std::vector<std::string>();
}
for ( std::string& option : options.front() ) {
if ( ! previous.empty() && ! func(previous, option) ) {
continue;
}
if ( options.size() == 1 ) {
return std::vector<std::string>(1, option);
}
std::vector<std::vector<std::string>> next_options(options.begin() + 1, options.end());
std::vector<std::string> result = amb(func, next_options, option);
if ( ! result.empty() ) {
result.emplace(result.begin(), option);
return result;
}
}
return std::vector<std::string>();
}
std::string Amb(std::vector<std::vector<std::string>> options) {
std::function<bool(std::string, std::string)> continues =
[](std::string before, std::string after) { return before.back() == after.front(); };
std::vector<std::string> amb_result = amb(continues, options, "");
return ( amb_result.empty() ) ? "No match found" : join(" ", amb_result);
}
int main() {
std::vector<std::vector<std::string>> words = { { "the", "that", "a" },
{ "frog", "elephant", "thing" },
{ "walked", "treaded", "grows" },
{ "slowly", "quickly" } };
std::cout << Amb(words) << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
that thing grows slowly
</pre>
Line 1,349 ⟶ 1,414:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
import extensions'routines;
// --- Joinable --
joinable(former,later) = (former[former.Length - 1] == later[0]);
Line 1,378 ⟶ 1,445:
}
};
// --- AmbValueCollection ---
class AmbValueCollection
{
object
constructor new(params object[] args)
{
}
seek(cond)
{
}
do(f)
{
var result :=
if (nil != result)
{
Line 1,408 ⟶ 1,477:
}
}
// --- ambOperator ---
singleton ambOperator
Line 1,414 ⟶ 1,485:
= AmbValueCollection.new(params args);
}
// --- Program ---
public program()
Line 1,425 ⟶ 1,498:
new object[]{"walked", "treaded", "grows"},
new object[]{"slowly", "quickly"})
.seek::(a,b,c,d => joinable(a,b) && joinable(b,c) && joinable(c,d) )
.do::(a,b,c,d) { console.printLine(a," ",b," ",c," ",d) }
}
catch(Exception e)
{
console.printLine
};
Line 1,924 ⟶ 1,997:
return s
end</syntaxhighlight>
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(function amb op res
(filter #(= res (.. .. op args))
(.. for vec (skip 2 args))))
(var safe= @(= (0 args)))
(var predicate (comp vec (map (juxt 0 -1)) flatten (skip 1) (partition 2) (map (.. safe=)) (.. and)))
(amb predicate true ["the" "that" "a"] ["frog" "elephant" "thing"] ["walked" "treaded" "grows"] ["slowly" "quickly"])
;returns [["that" "thing" "grows" "slowly"]]</syntaxhighlight>
=={{header|J}}==
Line 1,958 ⟶ 2,043:
│that│thing│grows│slowly│
└────┴─────┴─────┴──────┘</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class AmbTask {
public static void main(String[] aArgs) {
List<List<String>> words = List.of(
List.of( "the", "that", "a" ),
List.of( "frog", "elephant", "thing" ),
List.of( "walked", "treaded", "grows" ),
List.of( "slowly", "quickly" ) );
System.out.println(Amb(words));
}
private static String Amb(List<List<String>> aOptions) {
BiFunction<String, String, Boolean> continues = (before, after) -> before.endsWith(after.substring(0, 1));
List<String> ambResult = amb(continues, aOptions, "");
return ( ambResult.isEmpty() ) ? "No match found" : String.join(" ", ambResult);
}
private static List<String> amb(
BiFunction<String, String, Boolean> aBiFunction, List<List<String>> aOptions, String aPrevious) {
if ( aOptions.isEmpty() ) {
return new ArrayList<String>();
}
for ( String option : aOptions.get(0) ) {
if ( ! aPrevious.isEmpty() && ! aBiFunction.apply(aPrevious, option) ) {
continue;
}
if ( aOptions.size() == 1 ) {
return Stream.of(option).collect(Collectors.toList());
}
List<String> result = (ArrayList<String>) amb(aBiFunction, aOptions.subList(1, aOptions.size()), option);
if ( ! result.isEmpty() ) {
result.add(0, option);
return result;
}
}
return new ArrayList<String>();
}
}
</syntaxhighlight>
{{ out }}
<pre>
that thing grows slowly
</pre>
=={{header|JavaScript}}==
Line 2,301 ⟶ 2,448:
=={{header|langur}}==
This would build every valid set, but for the sample data, there's only one.
<syntaxhighlight lang="langur">val .wordsets = [
]
val .alljoin =
if last(.words[.i]) != first(.words[.i+1]): break = false
}}
# .amb expects 2 or more arguments
val .amb =
writeln join "\n", filter
</syntaxhighlight>
{{out}}
Line 4,210 ⟶ 4,357:
=={{header|REXX}}==
===version 1===
<syntaxhighlight lang="rexx"> /* REXX **************************************************************
* 25.08.2013 Walter Pachl derived from PL/I
Line 4,321 ⟶ 4,442:
End
Return</syntaxhighlight>
<pre>Input:
the frog walked slowly
that elephant treaded quickly
a thing grows
if trots
--> the elephant trots slowly
--> that thing grows slowly
--> if frog grows slowly</pre>
===version 2===
An assumption was made that equivalent lowercase and uppercase (Latin) letters are considered a match,
<br>although that isn't case here for these words (required by this task).
<syntaxhighlight lang="rexx">/*REXX program demonstrates the Amd operator, choosing a word from each set. */
@.1 = "the that a"
@.2 = "frog elephant thing"
@.3 = "walked treaded grows"
@.4 = "slowly quickly"
@.0 = 4 /*define the number of sets being ised.*/
call Amb 1 /*find all word combinations that works*/
exit /*stick a fork in it, we're all done. */
/*--------------------------------------------------------------------------------------*/
Amb: procedure expose @.; parse arg # x; arg . u /*ARG uppercases U value. */
if #>@.0 then do; y= word(u, 1) /*Y: is a uppercased U. */
do n=2 to words(u); ?= word(u, n)
if left(?, 1) \== right(y, 1) then return; y= ?
end /*n*/
say strip(x) /*¬show superfluous blanks.*/
end
do j=1 for words(@.#); call Amb #+1 x word(@.#, j) /*gen all combos recursively*/
end /*j*/; return</syntaxhighlight>
{{out|output|text= when using the default internal inputs:}}
<pre>that thing grows slowly</pre>
=={{header|Ring}}==
Line 5,134 ⟶ 5,288:
uBasic/4tH has limited support for arrays, so some workarounds are required to make this work.
<syntaxhighlight lang="text"> ' set up the arrays
Push
Push
Push
Push
' we'll reuse variable f ;-)
Proc _Ambassert (_Connect) ' now assert the function required
Line 5,173 ⟶ 5,327:
0 OK, 0:772
</pre>
=={{header|VBScript}}==
=====Implementation=====
Line 5,253 ⟶ 5,408:
{{trans|Go}}
Based on the 'alternative' version.
<syntaxhighlight lang="
var amb // recursive closure
Line 5,278 ⟶ 5,433:
[ "walked", "treaded", "grows" ],
[ "slowly", "quickly" ]
]
if (amb.call(wordsets, [])) {
|