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(‘ ’))