Amb: Difference between revisions

Content added Content deleted
(SETL)
(→‎SETL: Improved backtracking version and moved it in front)
Line 313: Line 313:


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

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

words := [amb(words): words in sets];
if exists lWord = words(i), rWord in {words(i+1)} |
lWord(#lWord) /= rWord(1) then
fail;
end if;

proc amb(words);
return arb {word in words | ok};
end proc;

end program;</lang>
Sadly ''ok'' and ''fail'' were only ever implemented in CIMS SETL, and are not in any compiler or interpreter that is available today, so this is not very useful as it stands.

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


Line 339: Line 357:


end program;</lang>
end program;</lang>
We cheat a bit here - this version of ''amb'' must be given the whole list of word sets, and that list is consumed recursively. It can't pick a word from an individual list.

===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>