<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
<H3><FONT style="FONT-SIZE: 10pt" color=#0c0c0c size=2>Hi, I am really new to haskell. I am reading "A gentle instruction to haskell" now. And I just cannot understand the chapter below. Is there anybody who can gives me some hints about why the pattern matching for "client" is so early? How does the pattern matching works here? </FONT></H3>
Thank you so much for answering my questions!<BR>
&nbsp;<BR>
Sincerely, <BR>
nemo<BR>
<H3><FONT color=#f79646>4.4<TT>&nbsp;&nbsp;</TT>Lazy Patterns</FONT></H3>
<FONT color=#f79646>There is one other kind of pattern allowed in Haskell. It is called a <EM>lazy pattern</EM>, and has the form <TT>~</TT>pat. Lazy patterns are <EM>irrefutable</EM>: matching a value v against <TT>~</TT>pat always succeeds, regardless of pat. Operationally speaking, if an identifier in pat is later "used" on the right-hand-side, it will be bound to that portion of the value that would result if v were to successfully match pat, and _|_ otherwise. </FONT>
<P><FONT color=#f79646>Lazy patterns are useful in contexts where infinite data structures are being defined recursively. For example, infinite lists are an excellent vehicle for writing <EM>simulation</EM> programs, and in this context the infinite lists are often called <EM>streams</EM>. Consider the simple case of simulating the interactions between a server process <TT>server </TT>and a client process <TT>client</TT>, where <TT>client</TT> sends a sequence of <EM>requests</EM> to <TT>server</TT>, and <TT>server</TT> replies to each request with some kind of <EM>response</EM>. This situation is shown pictorially in Figure 2. (Note that <TT>client</TT> also takes an initial message as argument.) </FONT>
<DIV align=center><FONT color=#f79646><IMG alt="Client Server Example" src="http://haskell.org/tutorial/fig2.gif"> </FONT>
<H4><FONT color=#f79646>Figure 2</FONT></H4></DIV><FONT color=#f79646>Using streams to simulate the message sequences, the Haskell code corresponding to this diagram is: </FONT><TT><BR><BR><FONT color=#f79646>reqs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;client&nbsp;init&nbsp;resps<BR>resps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;server&nbsp;reqs<BR><BR></FONT></TT><FONT color=#f79646>These recursive equations are a direct lexical transliteration of the diagram. </FONT>
<P><FONT color=#f79646>Let us further assume that the structure of the server and client look something like this: </FONT><TT><BR><BR><FONT color=#f79646>client&nbsp;init&nbsp;(resp:resps)&nbsp;=&nbsp;init&nbsp;:&nbsp;client&nbsp;(next&nbsp;resp)&nbsp;resps<BR>server&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(req:reqs)&nbsp;&nbsp;&nbsp;=&nbsp;process&nbsp;req&nbsp;:&nbsp;server&nbsp;reqs<BR><BR></FONT></TT><FONT color=#f79646>where we assume that <TT>next</TT> is a function that, given a response from the server, determines the next request, and <TT>process</TT> is a function that processes a request from the client, returning an appropriate response. </FONT>
<P><FONT color=#f79646>Unfortunately, this program has a serious problem: it will not produce any output! The problem is that <TT>client</TT>, as used in the recursive setting of <TT>reqs</TT> and <TT>resps</TT>, attempts a match on the response list before it has submitted its first request! In other words, the pattern matching is being done "too early." One way to fix this is to redefine <TT>client</TT> as follows: </FONT><TT><BR><BR><FONT color=#f79646>client&nbsp;init&nbsp;resps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;init&nbsp;:&nbsp;client&nbsp;(next&nbsp;(head&nbsp;resps))&nbsp;(tail&nbsp;resps)<BR><BR></FONT></TT><FONT color=#f79646>Although workable, this solution does not read as well as that given earlier. A better solution is to use a lazy pattern: </FONT><TT><BR><BR><FONT color=#f79646>client&nbsp;init&nbsp;~(resp:resps)&nbsp;=&nbsp;init&nbsp;:&nbsp;client&nbsp;(next&nbsp;resp)&nbsp;resps<BR><BR></FONT></TT><FONT color=#f79646>Because lazy patterns are irrefutable, the match will immediately succeed, allowing the initial request to be "submitted", in turn allowing the first response to be generated; the engine is now "primed", and the recursion takes care of the rest.</FONT><BR>
<FONT color=#0c0c0c></FONT>&nbsp;<BR><br /><hr />使用新一代 Windows Live Messenger 轻松交流和共享! <a href='http://www.windowslive.cn' target='_new'>立刻下载!</a></body>
</html>