1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html >
<head><title>Lazy programming</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)">
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)">
<!-- 3,html -->
<meta name="src" content="nipguide.tex">
<link rel="stylesheet" type="text/css" href="nipguide.css">
</head><body
>
<!--l. 855--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese31.html" >next</a>] [<a
href="nipguidese28.html" >prev</a>] [<a
href="nipguidese28.html#tailnipguidese28.html" >prev-tail</a>] [<a
href="nipguidese28.html#tailnipguidese29.html">tail</a>] [<a
href="nipguidech6.html#nipguidese30.html" >up</a>] </p></div>
<h3 class="sectionHead"><span class="titlemark">6.8 </span> <a
id="x40-720006.8"></a>Lazy programming</h3>
<!--l. 856--><p class="noindent" ><a name="nip_label_sec:lazy"></a>
<!--l. 858--><p class="indent" > <span
class="phvr7t-x-x-80">nip2</span>’s programming language is <span
class="ptmri7t-">lazy</span>, that is, it delays
evaluation as long as it possibly can. For example, <span
class="phvr7t-x-x-80">error </span>is a
function which immediately halts execution of your
function and pops up an alert window. So:
<div class="verbatim" id="verbatim-61">
12 + error "wombat!"
</div>
<!--l. 864--><p class="nopar" >
<!--l. 866--><p class="noindent" >Has no value: this expression will halt with an error
message. However:
<div class="verbatim" id="verbatim-62">
false && error "lasagne!"
</div>
<!--l. 871--><p class="nopar" >
<!--l. 873--><p class="noindent" >Will evaluate to <span
class="phvr7t-x-x-80">false</span>, since <span
class="phvr7t-x-x-80">nip2 </span>knows after looking at the
left-hand-side of <span
class="phvr7t-x-x-80">&& </span>that the result must be <span
class="phvr7t-x-x-80">false</span>, and so does
not evaluate the right-hand-side.
<div class="verbatim" id="verbatim-63">
[12, error "hot chilli!"] ? 0 == 12
</div>
<!--l. 880--><p class="nopar" >
<!--l. 882--><p class="noindent" >This also evaluates completely, since the second element of
the list is never used, and therefore never evaluates.
<!--l. 886--><p class="indent" > Things become more confusing when you start calling
functions, since the arguments to a function call are also
not evaluated until the function needs that value. For
example:
<div class="verbatim" id="verbatim-64">
foldr (error "boink!") 2 [] == 2
</div>
<!--l. 892--><p class="nopar" >
<!--l. 894--><p class="noindent" >Again, this evaluates successfully, since the function is
never used by <span
class="phvr7t-x-x-80">foldr</span>.
<!--l. 898--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese31.html" >next</a>] [<a
href="nipguidese28.html" >prev</a>] [<a
href="nipguidese28.html#tailnipguidese28.html" >prev-tail</a>] [<a
href="nipguidese29.html" >front</a>] [<a
href="nipguidech6.html#nipguidese30.html" >up</a>] </p></div>
<!--l. 898--><p class="indent" > <a
id="tailnipguidese29.html"></a>
</body></html>
|