Again, it depends how takeWhile is implemented -- if it's not tail
recursive, the compiler will usually manage to run such functions in
constant space.
Bob
On 30 Sep 2005, at 16:02, gary ng wrote:
> Once again, many thanks to all who taught me about
> this small little problem. Don't even know there is
> init/last and thought there is only head/tail.
>> But just for my curiosity, would the takeWhile still
> store the intermediate result till my result is
> reached ? If so, and my list is really very long(and I
> need to go to 1/2 of its length), I would still use a
> lot more memory than imperative method or even the
> foldl one(where in both case, I just take one element)
> ?
>> --- Henning Thielemann <lemming at henning-thielemann.de>
> wrote:
>>>> No problem:
>> last (takeWhile (<maxX) (scanl (+) 0 xs))
>> Convinced?
>>>> The first sum which exceeds the limit could be
>> computed with
>> head (dropWhile (<=maxX) (scanl (+) 0 xs))
>>>>>>>>>>> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005
>http://mail.yahoo.com> _______________________________________________
> Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org>http://www.haskell.org/mailman/listinfo/haskell-cafe>