declare
proc {SMerge Xs Ys ?R}
local T in
T = Xs#Ys
case T
of nil#Ys then R = Ys
else
case T of Xs#nil then R = Xs
else
case T of
(X|Xr)#(Y|Yr) then
local K in
K = X=<Y
if K then S in
R = X|S
{SMerge Xr Ys S}
else S in
R = Y|S
{SMerge Xr Ys S}
end
end
end
end
end
end
end
Notice the usage of dataflow variable S to keep recursive call to be last so as to make it tail-recursive, this trick is used when a function call is made inside a data-structure(for reference read the section "Function calls in data structures" in the book).
No comments:
Post a Comment