PFDS Chapter #04 Examples in Oz
% Utility functions for tests
proc {StreamTest F}
A = {F.append [1 2 3] [4 5]}
in
{Browse A}
{Browse {F.take A 1}}
{Browse {F.take A 2}}
{Browse {F.take A 3}}
{Browse {F.drop A 1}}
{Browse {F.drop A 2}}
{Browse {F.drop A 3}}
{Browse {F.take {Stream.reverse A} 6}}
end
% 4.2 Stream
STREAM =
functor
export
append : Append
take : Take
drop : Drop
reverse : Reverse
fromList : FromList
toList : ToList
define
fun lazy {Append L1 L2}
case L1
of nil then L2
[] H|T then H|{Append T L2}
end
end
fun {Take L N}
if N == 0 then
nil
else
case L
of H|T then H|{Take T N-1}
[] nil then nil
end
end
end
fun {Drop L N}
if N == 0 then
L
else
case L
of H|T then {Drop T N-1}
[] nil then nil
end
end
end
fun lazy {Reverse L}
fun {Iter L1 L2}
case L1
of H|T then {Iter T H|L2}
[] nil then L2
end
end
in
{Iter L nil}
end
fun {FromList L} L end
fun {ToList L} L end
end
[Stream] = {Module.apply [STREAM]}
{StreamTest Stream}
|