% Solution de la question 1 des examens FSAB1402 du janvier 2008 % Peter Van Roy % Question 1 du 10 janvier 2008 declare proc {Split Xs K Ys Zs} proc {SplitLoop Xs I Ys Zs} case Xs of nil then Ys=nil Zs=nil [] X|Xr then if I mod K == 0 then Yr in Ys=X|Yr {SplitLoop Xr I+1 Yr Zs} else Zr in Zs=X|Zr {SplitLoop Xr I+1 Ys Zr} end end end in {SplitLoop Xs 0 Ys Zs} end local Ys Zs in {Split [a b c d e f g h i j] 3 Ys Zs} {Browse Ys#Zs} end declare proc {InvSplit Ys Zs K Xs} proc {InvSplitLoop Ys Zs I Xs} if I mod K == 0 then case Ys of nil then Xs=Zs [] Y|Yr then Xr in Xs=Y|Xr {InvSplitLoop Yr Zs I+1 Xr} end else case Zs of nil then Xs=Ys [] Z|Zr then Xr in Xs=Z|Xr {InvSplitLoop Ys Zr I+1 Xr} end end end in {InvSplitLoop Ys Zs 0 Xs} end local Xs in {InvSplit [a d g j] [b c e f h i] 3 Xs} {Browse Xs} end % Question 1 du 18 janvier 2008 declare fun {Roulez Xs Ys} fun {RoulezLoop As Bs} case As#Bs of nil#nil then Xs.1|Ys.1|nil [] As#nil then {RoulezLoop As Ys} [] nil#Bs then {RoulezLoop Xs Bs} [] (A|Ar)#(B|Br) then A|B|{RoulezLoop Ar Br} end end in {RoulezLoop Xs Ys} end {Browse {Roulez [a b c] [1 2]}} declare fun {SaufAvantDernier Xs} case Xs of nil then raise listeTropCourte(Xs) end [] X|nil then raise listeTropCourte(Xs) end [] X1|X2|nil then X2|nil [] X|Xr then X|{SaufAvantDernier Xr} end end {Browse {SaufAvantDernier [a b c]}}