<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi,<div><br></div><div>I am confused about this piece of explanation in Real World Haskell.</div><div><br></div><div><span class="Apple-style-span" style="font-family: verdana, sans-serif; "><pre id="append.hs:append" class="programlisting" style="font-family: monospace; font-weight: normal; background-image: url(http://book.realworldhaskell.org/support/figs/source.png); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: rgb(240, 244, 255); border-top-color: rgb(180, 186, 234); border-right-color: rgb(180, 186, 234); border-bottom-color: rgb(180, 186, 234); border-left-color: rgb(180, 186, 234); border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; font-size: medium; padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 70px; overflow-x: hidden; overflow-y: hidden; background-position: 10px 10px; background-repeat: no-repeat no-repeat; ">-- file: ch08/append.hs
(++) :: [a] -&gt; [a] -&gt; [a]

(x:xs) ++ ys = x : (xs ++ ys)
[]     ++ ys = ys</pre><p id="x_rE1"><a name="x_rE1"></a>In a strict language, if we evaluate&nbsp;<code class="code" style="font-family: monospace; font-weight: normal; ">"foo" ++ "bar"</code>, the entire list is constructed, then returned. Non-strict evaluation defers much of the work until it is needed.</p><p id="x_bG"><a name="x_bG"></a>If we demand an element of the expression&nbsp;<code class="code" style="font-family: monospace; font-weight: normal; ">"foo" ++ "bar"</code>, the first pattern of the function's definition matches, and we return the expression&nbsp;<code class="code" style="font-family: monospace; font-weight: normal; ">x : (xs ++ ys)</code>. Because the&nbsp;<code class="code" style="font-family: monospace; font-weight: normal; ">(:)</code>&nbsp;constructor is non-strict, the evaluation of&nbsp;<code class="code" style="font-family: monospace; font-weight: normal; ">xs ++ ys</code>&nbsp;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&nbsp;<code class="varname" style="font-family: monospace; font-weight: normal; ">x</code>, 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.&nbsp;</p></span><div>When ('f' : "oo") ++ "bar" becomes 'f' : ("oo" ++ "bar") and then becomes 'f' : ('o' : ("o" ++ "bar")), we still need 'f', don't we?</div><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div><br class="Apple-interchange-newline">Best regards,<br class="Apple-interchange-newline">Zhi-Qiang Lei</div><div><a href="mailto:zhiqiang.lei@gmail.com">zhiqiang.lei@gmail.com</a></div></span>
</div>
<br></div></body></html>