rweikusat/amb
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
Some toy code I wrote to solve the problem defined at https://rosettacode.org/wiki/Amb It's basically: Given a list of lists of words and a predicate function, find a combination of one word from each word list which satisfies the predicate by doing an exhaustive search ('backtracking') of the set of all possible combinations. The specified predicate is that each word of the solution except the first must start with the letter the last one ended with. amb.pl is a Perl implementation of the first idea I had for this, namely, the operator takes a predicate function and a list of words. The predicate function either returns a value or throws an exception. In case of the former, the returned value is the sought-after solution. Otherwise, the operator invokes itself recursively to try to next world. For the actual problem, ie, for more than one wordlist, the predicate function must do nested amb calls as needed. amb-2.pl is another Perl implementation. This time, the operator takes a list of word lists and a predicate function and implements a straight-forward recursive exhaustive search of all possible combinations. amb.c is C implementation of the same algorithm based on using null-pointer terminated arrays for the individual word lists, the result list and the list of word lists. It shares the list of word lists among all recursive invocations (as opposed to amd-2.pl, which creates a copy of it at each level) but creates a copy of the "combination so far" at each level. amb-iter.c is an iterative implementation in C. It's a standard transliteration of a recursive into an iterative algorithm using an explicit stack for the per-wordlist positions instead of storing these implicity on the callstack. An inner loop deals with the special-case of the final list. Two more inner loops implement the yoyo-style down - up - down movements on this stack which lead to an exhaustive search of the space of possible solutions.