RosettaCodeData/Task/Amb/11l/amb.11l
2023-07-01 13:44:08 -04:00

29 lines
812 B
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

F amb(comp, options, prev = ) -> Array[String]
I options.empty
R []
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.
V 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 []
V sets = [[the, that, a],
[frog, elephant, thing],
[walked, treaded, grows],
[slowly, quickly]]
V result = amb((s, t) -> s.last == t[0], sets)
print(result.join( ))