Skip to content

Instantly share code, notes, and snippets.

@tetsu-miyagawa
Created November 18, 2015 14:33
Show Gist options
  • Save tetsu-miyagawa/f01c3ab708b516a7755b to your computer and use it in GitHub Desktop.
Save tetsu-miyagawa/f01c3ab708b516a7755b to your computer and use it in GitHub Desktop.
CTMCP Section 2.9 Exercise 6
local SMerge in
fun {SMerge Xs Ys}
case Xs#Ys
of nil#Ys then Ys
[] Xs#nil then Xs
[] (X|Xr)#(Y|Yr) then
if X=<Y then X|{SMerge Xr Ys}
else Y|{SMerge Xs Yr} end
end
end
{Browse {SMerge [1 3 5 7 9] [4 6 8 10 12]}}
end
local SMerge R in
SMerge = proc {$ Xs Ys ?R}
case Xs of nil then R=Ys
else
case Ys of nil then R=Xs
else
case Xs of X|Xr then
case Ys of Y|Yr then
local M in
if X=<Y then
R = X|M
{SMerge Xr Ys M}
else
R = Y|M
{SMerge Xs Yr M}
end
end
end
end
end
end
end
{PSMerge [1 3 5 7 9] [4 6 8 10 12] R}
{Browse R}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment