Courses/CS 460/Fall 2005/Homework/Sean Tseng/Oct 8
From CSWiki
< Courses | CS 460 | Fall 2005 | Homework | Sean Tseng
Contents |
[edit] Homework 2
[edit] Take
local A B
proc {Take Xs I Ys }
if I == 0 then Ys = nil
elseif I > {List.length Xs} then Ys = Xs
else Zs Zr in
Xs = Zs|Zr
if I == 1 then Ys = [Zs]
else Ys = Zs | {Take Zr (I-1)}
end
end
end in
A = [6 2 3 7]
{Take A 1 B}
{Browse B}
end
[edit] Drop
local A B
proc {Drop Xs I Ys}
if I == 0 then Ys = Xs
elseif I >= {List.length Xs} then Ys = nil
else Zr in
Xs = _|Zr
Ys = {Drop Zr (I-1)}
end
end in
A = [6 2 3 7]
{Drop A 2 B}
{Browse B}
end
[edit] Sub
local A B
proc {Sub Xs Ys B}
case Xs#Ys of
_#nil then B = true
[]nil#_ then B = false
[](X1|Xr) # (Y1|Yr) then
if X1 == Y1 then B = {Sub Xr Yr}
else B = {Sub Xr Ys}
end
end
end in
A = [a 3 4 g]
{Sub A [3 g] B}
{Browse B}
end
[edit] All
local X Y
proc {All Xs P B}
case Xs of
nil then B = true
[]X1|Xr then if {P X1} == false then B = false
else B = {All Xr P}
end
end
end in
X = [4 ~2 2 1]
{All X fun {$ A} if A < 5 then true else false end end Y}
{Browse Y}
end
[edit] Some
local X Y
proc {Some Xs P B}
case Xs of
nil then B = false
[]X1|Xr then if {P X1} == true then B = true
else B = {Some Xr P}
end
end
end in
X = [42 6 21]
{Some X fun {$ A} if A < 5 then true else false end end Y}
{Browse Y}
end

