🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.funkcyjne
Article #33

Re: Przepełnienie stosu w Ocamlu.

#33
From: Tomasz Zielonka
Date: Sat, 07 Dec 2002 15:11
71 lines
3354 bytes
Mariusz K. Grzeca napisał:
> On Fri, 6 Dec 2002, Tomek Czajka wrote:
> 
>> W jezyku leniwym odpowiednikiem takiego for-a jest fold
> 
> Nie wiem w jakim. W znanych mi nie ma funkcji fold dla list. Są między
> innymi foldl i foldr.

Chyba niepotrzebnie się czepiasz szczegółów.

>> W jezyku funkcyjnym leniwym bedzie to sie odbywalo w pamieci stalej.
> 
> Nie będzie. Prześledź sobie jak wygląda redukcja wyrażenia:
> 
> foldl (+) 0 [a1, a2, a3, a4, a5, ...]
> 
> foldl (+) (0 + a1) [a2, a3, a4, a5, ...]
> foldl (+) (0 + a1 + a2) [a3, a4, a5, ...]
> foldl (+) (0 + a2 + a3 + a4) [a5, ...]
> ...

> Po pierwsze foldl generuje wyrażenie reprezentowane przez łańcuch
> thunków o długości liniowej względem rozmiaru danych wejściowych.

> Po drugie ewaluacja tego wyrażenia wymaga liniowego stosu :)

A to niby dlaczego?

Nie będę powtarzał tego, co już zostało powiedziane:

     * From: Ketil Malde
     * Subject: Re: foldl, tail recursion, and strictness (was: stack
     * overflow)
     * Date: Mon, 26 Feb 2001 06:42:57 -0800

     ---------------------------------------------------------------------------

 "C.Reinke" <C.Reinke@ukc.ac.uk> writes:

 > So foldl is indeed tail recursive, but this doesn't help if its
 > operator isn't strict because the tail recursion only builds up the
 > expression to be evaluated. Making strictness explicit by defining a
 > variant of foldl that evaluates its accumulator at each step helps to
 > solve the problem and documents programmer intentions.

 > This is quite common a trap for Haskellers to fall into

 It makes me wonder if it's common enough that strict foldl should be
 the default?  Are there (many) cases where a partial foldl makes
 sense?

 -kzm

Foldl gorliwy na argumencie akumulatora można zapisać np. tak:

foldl' :: (a -> b -> a) -> a -> [b] -> a
foldl' f acc (x:xs) = (foldl' f $! (f acc x)) xs
foldl' _ acc []     = acc

> Po trzecie foldl i foldr, lub ich odpowiedniki są również w gorliwych
> językach funkcyjnych :)

Tak, ale one są gorliwe aż do przesady, więc trudno mówić, że jest to
odpowiednik pętli for. Pętla for nie musi budować w pamięci całej listy
elementów po których będzie iterować.

pozdrawiam,
tomek

-- 
.signature: Too many levels of symbolic links
Technical Details
Message-ID: <slrnav43tc.og1.t.zielonka@zodiac.mimuw.edu.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!news.man.torun.pl!news.man.poznan.pl!news.internetia.pl!mimuw.edu.pl!news.mimuw.edu.pl!not-for-mail
References: <asg3hf$25q$1@kenny.mimuw.edu.pl> <3DF10715.9A91D4D8@mimuw.edu.pl> <Pine.LNX.4.44L.0212070001400.22165-100000@sokrates.mimuw.edu.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!news.man.torun.pl!news.man.poznan.pl!news.internetia.pl!mimuw.edu.pl!news.mimuw.edu.pl!not-for-mail
From: Tomasz Zielonka <t.zielonka@students.mimuw.edu.pl>
Newsgroups: pl.comp.lang.funkcyjne
Subject: Re: Przepełnienie stosu w Ocamlu.
Date: Sat, 7 Dec 2002 15:11:43 +0000 (UTC)
Organization: MIMUW
Lines: 71
Message-ID: <slrnav43tc.og1.t.zielonka@zodiac.mimuw.edu.pl>
References: <asg3hf$25q$1@kenny.mimuw.edu.pl> <3DF10715.9A91D4D8@mimuw.edu.pl> <Pine.LNX.4.44L.0212070001400.22165-100000@sokrates.mimuw.edu.pl>
Reply-To: t.zielonka@students.mimuw.edu.pl
NNTP-Posting-Host: zodiac.mimuw.edu.pl
Mime-Version: 1.0
X-Trace: kenny.mimuw.edu.pl 1039273903 15108 10.1.1.1 (7 Dec 2002 15:11:43 GMT)
X-Complaints-To: abuse@news.mimuw.edu.pl
NNTP-Posting-Date: Sat, 7 Dec 2002 15:11:43 +0000 (UTC)
User-Agent: slrn/0.9.7.3 (Linux)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit