-
Notifications
You must be signed in to change notification settings - Fork 11
/
amb.nim
31 lines (23 loc) · 939 Bytes
/
amb.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sugar, strutils
proc amb(comp: proc(a, b: string): bool, options: seq[seq[string]],
prev: string = ""): seq[string] =
if options.len == 0: return @[]
for opt in options[0]:
# If this is the base call, prev is nil and we need to continue.
if prev.len != 0 and not comp(prev, opt): continue
# Take care of the case where we have no options left.
if options.len == 1: return @[opt]
# Traverse into the tree.
let res = amb(comp, options[1..options.high], opt)
# If it was a failure, try the next one.
if res.len > 0: return opt & res # We have a match
return @[]
const sets = @[@["the", "that", "a"],
@["frog", "elephant", "thing"],
@["walked", "treaded", "grows"],
@["slowly", "quickly"]]
let result = amb((s: string, t: string) => (s[s.high] == t[0]), sets)
if result.len == 0:
echo "No matches found!"
else:
echo result.join " "