% $Id: HailstoneLazy.oz,v 1.2 2008/03/28 22:27:31 leavens Exp leavens $ % Compare this (lazy) version with HailstoneDD.oz \insert 'HailstoneMax.oz' declare % Return the IStream N|N+1|... fun lazy {From N} N|{From N+1} end % Return the list [1 2 3 ... N] fun {Count N} {List.take {From 1} N} end % Return the IStream of pairs of the form E#{F E}, % in order, for each element E of the list argument. fun lazy {LGraph E|Es F} (E#{F E})|{LGraph Es F} end % Return the peak values of the IStream argument. % The IStream is assumed to be a list of pairs. fun {LPeaks List} fun lazy {PeakIter (Arg#Val)|Ps MaxSoFar} if Val > MaxSoFar then (Arg#Val) | {PeakIter Ps Val} else {PeakIter Ps MaxSoFar} end end in {PeakIter List 0} end % Return an IStream of the first N HailstoneMax arguments and values fun {LGraphHailstoneMax N} Output in thread Output={List.take {LGraph {From 1} HailstoneMax} N} end Output end % Return an IStream of the peak arguments and values fun {LGraphMaxPeaks NumberSought} Output in thread Output={List.take {LPeaks {LGraph {From 1} HailstoneMax}} NumberSought} end Output end