RosettaCodeData/Task/Pattern-matching/Phix/pattern-matching-1.phix

31 lines
840 B
Plaintext

function match_one(sequence key, object t)
sequence res = {}
if sequence(t)
and length(key)==length(t) then
for i=1 to length(key) do
object ki = key[i], ti = t[i]
if sequence(ki) and not string(ki) then
sequence r2 = match_one(ki,ti)
if r2={} then res = {} exit end if
res &= r2
else
if ki=0 then
res = append(res,ti)
else
if ki!=ti then res = {} exit end if
end if
end if
end for
end if
return res
end function
/*global*/ function match_algebraic(sequence set, t)
sequence s
for i=1 to length(set) do
s = match_one(set[i],t)
if length(s) then exit end if
end for
return s
end function