% $Id: HailstonePeaks.oz,v 1.1 2007/10/29 20:05:01 leavens Exp leavens $ \insert 'HailstoneMax.oz' declare % Return the list [1 2 3 .. N] fun {Count N} for I in 1 .. N collect: C do {C I} end end % Return the list of pairs of the form E#{F E}, % in order, for each element E of the list argument. fun {Graph List F} {Map List fun {$ E} E#{F E} end} end % Return the peak values of the list argument. % The list is assumed to be a list of pairs. fun {Peaks List} fun {PeakIter List MaxSoFar} case List of nil then nil [] (Arg#Val)|Ps then if Val > MaxSoFar then (Arg#Val) | {PeakIter Ps Val} else {PeakIter Ps MaxSoFar} end end end in {PeakIter List 0} end % Return a list of the peak arguments and values fun {GraphMaxPeaks SearchLimit} Numbers InStream OutStream in thread Numbers = {Count SearchLimit} end thread InStream = {Graph Numbers HailstoneMax} end thread OutStream = {Peaks InStream} end OutStream end