Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Question about lazy evaluation ( anyzhen )
   2. Re:  Question about lazy evaluation (Thomas)


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

Message: 1
Date: Thu, 8 Sep 2011 14:08:06 +0800
From: " anyzhen " <[email protected]>
Subject: Re: [Haskell-beginners] Question about lazy evaluation
To: " Zhi-Qiang Lei " <[email protected]>, " Haskell Beginer "
        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="gbk"

i think  , garbage collector reclaim the ?f' just only  when "foo" can't access 
.because i think  the 'f' is shared between ?foo? with "foobar"( "foo" ++ "bar" 
).


Zhen Jiang 
[email protected] 
 
 
------------------ Original ------------------
From: "Zhi-Qiang Lei"; 
Date: 2011?9?8?(???) ??1:56
To: "Haskell Beginer"; 
Subject: [Haskell-beginners] Question about lazy evaluation

 
Hi,

I am confused about this piece of explanation in Real World Haskell.


-- file: ch08/append.hs (++) :: [a] -> [a] -> [a] (x:xs) ++ ys = x : (xs ++ ys) 
[]     ++ ys = ys
In a strict language, if we evaluate "foo" ++ "bar", the entire list is 
constructed, then returned. Non-strict evaluation defers much of the work until 
it is needed.

If we demand an element of the expression "foo" ++ "bar", the first pattern of 
the function's definition matches, and we return the expression x : (xs ++ ys). 
Because the (:) constructor is non-strict, the evaluation of xs ++ ys can be 
deferred: we generate more elements of the result at whatever rate they are 
demanded. When we generate more of the result, we will no longer be using x, so 
the garbage collector can reclaim it. Since we generate elements of the result 
on demand, and do not hold onto parts that we are done with, the compiler can 
evaluate our code in constant space. 
When ('f' : "oo") ++ "bar" becomes 'f' : ("oo" ++ "bar") and then becomes 'f' : 
('o' : ("o" ++ "bar")), we still need 'f', don't we?
 
Best regards,
Zhi-Qiang Lei
[email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110908/811485a6/attachment-0001.htm>

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

Message: 2
Date: Thu, 08 Sep 2011 10:20:38 +0200
From: Thomas <[email protected]>
Subject: Re: [Haskell-beginners] Question about lazy evaluation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi!

Well, in your example you start with "foo" and "bar" and combine them to 
"foobar". The interesting part is that at the same rate you construct 
the result you're deconstructing the input.
So:
    "foo" ++ "bar"   ->  "foo", "bar", "" (space for result)
    'f' : ("oo" ++ "bar")  -> "oo", "bar", "f"
    'f' : ('o' : ("o" ++ "bar")) -> "o", "bar", "fo"
    ...

As you can see the space requirements are constant.

HTH,
Thomas

On 08.09.2011 07:56, Zhi-Qiang Lei wrote:
> Hi,
>
> I am confused about this piece of explanation in Real World Haskell.
>
> -- file: ch08/append.hs
> (++) :: [a] ->  [a] ->  [a]
>
> (x:xs) ++ ys = x : (xs ++ ys)
> []     ++ ys = ys
> In a strict language, if we evaluate "foo" ++ "bar", the entire list is 
> constructed, then returned. Non-strict evaluation defers much of the work 
> until it is needed.
>
> If we demand an element of the expression "foo" ++ "bar", the first pattern 
> of the function's definition matches, and we return the expression x : (xs ++ 
> ys). Because the (:) constructor is non-strict, the evaluation of xs ++ ys 
> can be deferred: we generate more elements of the result at whatever rate 
> they are demanded. When we generate more of the result, we will no longer be 
> using x, so the garbage collector can reclaim it. Since we generate elements 
> of the result on demand, and do not hold onto parts that we are done with, 
> the compiler can evaluate our code in constant space.
>
> When ('f' : "oo") ++ "bar" becomes 'f' : ("oo" ++ "bar") and then becomes 'f' 
> : ('o' : ("o" ++ "bar")), we still need 'f', don't we?
>
> Best regards,
> Zhi-Qiang Lei
> [email protected]
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 39, Issue 8
****************************************

Reply via email to