38 lines
986 B
Text
38 lines
986 B
Text
function amb1(sequence sets, object res=0, integer idx=1)
|
|
integer ch = 0
|
|
integer pass = 0
|
|
if idx>length(sets) then
|
|
pass = 1
|
|
else
|
|
if res=0 then
|
|
res = repeat(0,length(sets))
|
|
else
|
|
ch = sets[idx-1][res[idx-1]][$]
|
|
end if
|
|
for k=1 to length(sets[idx]) do
|
|
if ch=0 or sets[idx][k][1]=ch then
|
|
res[idx] = k
|
|
{pass,res} = amb1(sets,res,idx+1)
|
|
if pass then exit end if
|
|
end if
|
|
end for
|
|
end if
|
|
return {pass,res}
|
|
end function
|
|
|
|
sequence sets = {{"the","that","a"},
|
|
{"frog","elephant","thing"},
|
|
{"walked","treaded","grows"},
|
|
{"slowly","quickly"}}
|
|
integer pass
|
|
sequence res
|
|
{pass,res} = amb1(sets)
|
|
if pass then
|
|
puts(1,"success: ")
|
|
for i=1 to length(sets) do
|
|
res[i] = sets[i][res[i]]
|
|
end for
|
|
?res
|
|
else
|
|
puts(1,"failure\n")
|
|
end if
|