Amb: Difference between revisions

Content deleted Content added
→‎{{Header|Haskell}}: removed irrelevant aspects
SETL
Line 311: Line 311:
(list w-1 w-2 w-3 w-4))
(list w-1 w-2 w-3 w-4))
</lang>
</lang>

=={{Header|SETL}}==
<lang SETL>program amb;

sets := unstr('[{the that a} {frog elephant thing} {walked treaded grows} {slowly quickly}]');

print(amb(sets));

proc amb(sets);
return amb1([], {}, sets);
end proc;

proc amb1(prev, mbLast, sets);
if sets = [] then
return prev;
else
words fromb sets;
if exists word in words |
(forall last in mbLast |
last(#last) = word(1)) and
(exists sentence in {amb1(prev with word, {word}, sets)} |
true) then
return sentence;
end if;
end if;
end proc;

end program;</lang>

===Alternate version (uses [http://www.setl-lang.org/wiki/index.php/Backtracking backtracking])===
<lang SETL>program amb;

sets := unstr('[{the that a} {frog elephant thing} ' +
'{walked treaded grows} {slowly quickly}]');

print(amb(sets));

proc amb(sets);
sentence := [];
(for words in sets)
if exists word in words |
ok and (sentence = [] or
word(1) = (reverse sentence(#sentence))(1)) then
sentence with:= word;
else
fail;
end if;
end;
return sentence;
end proc;

end program;</lang>