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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html >
<head><title>Evaluation</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. 267--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese28.html" >next</a>] [<a
href="nipguidese25.html" >prev</a>] [<a
href="nipguidese25.html#tailnipguidese25.html" >prev-tail</a>] [<a
href="#tailnipguidese26.html">tail</a>] [<a
href="nipguidech6.html#nipguidese26.html" >up</a>] </p></div>
<h3 class="sectionHead"><span class="titlemark">6.5 </span> <a
id="x36-620006.5"></a>Evaluation</h3>
<!--l. 269--><p class="noindent" ><span
class="phvr7t-x-x-80">nip2 </span>calculates the value of an expression by using the
definitions you entered to successively reduce the expression
until it becomes one of the base types. Sometimes there is a
choice as to which part of the expression will be reduced
next — <span
class="phvr7t-x-x-80">nip2 </span>will always choose to reduce the leftmost,
outermost part of the expression first.
<!--l. 275--><p class="indent" > For example, consider this definition:
<div class="verbatim" id="verbatim-29">
factorial n
 <br />  = n ⋆ factorial (n - 1), n > 1
 <br />  = 1
</div>
<!--l. 281--><p class="nopar" >
<!--l. 283--><p class="indent" > And here’s how <span
class="phvr7t-x-x-80">nip2 </span>will evaluate the expression <span
class="phvr7t-x-x-80">factorial</span>
<span
class="phvr7t-x-x-80">3</span>:
<div class="verbatim" id="verbatim-30">
factorial 3 -->
 <br />  3 > 1 -->
 <br />  true
 <br />3 ⋆ factorial (3 - 1) -->
 <br />  (3 - 1) > 1 -->
 <br />  2 > 1 -->
 <br />  true
 <br />3 ⋆ (2 ⋆ factorial (2 - 1)) -->
 <br />  (2 - 1) > 1 -->
 <br />  1 > 1 -->
 <br />  false
 <br />3 ⋆ (2 ⋆ 1) -->
 <br />3 ⋆ 2 -->
 <br />6
</div>
<!--l. 300--><p class="nopar" >
<!--l. 302--><p class="noindent" >Note how <span
class="phvr7t-x-x-80">nip2 </span>delays evaluating parameters to functions
until they are needed, but still shares the result. <span
class="phvr7t-x-x-80">3 - 1 </span>is only
evaluated once, for example, even though the result is used
three times. <span
class="phvr7t-x-x-80">nip2 </span>has a trace window: click on <span
class="phvr7t-x-x-80">Debug </span>/ <span
class="phvr7t-x-x-80">Trace</span>
in the program window and check the <span
class="phvr7t-x-x-80">View </span>/ <span
class="phvr7t-x-x-80">Operators </span>menu
item.
<!--l. 309--><p class="indent" > The advantage of this style of computation over
conventional imperative programming languages
is that you can reason about your program
mathematically<span class="footnote-mark"><a
href="nipguide37.html#fn1x6"><sup class="textsuperscript">1</sup></a></span><a
id="x36-62001f1"></a> .
<!--l. 319--><p class="indent" > This isn’t the best way to write a factorial function.
A function with lots of recursive calls can be hard to
understand — it’s much better to use one of the higher order
functions from the standard environment to encapsulate the
type of recursion you want to use.
<!--l. 324--><p class="indent" > The clearest definition for factorial is probably:
<div class="verbatim" id="verbatim-31">
factorial n = product [1..n]
</div>
<!--l. 328--><p class="nopar" >
<!--l. 330--><p class="noindent" >See <span
class="cmsy-10"></span><a
href="nipguidese27.html#x38-680006.6.5">6.6.5<!--tex4ht:ref: sec:listsyntax --></a> for an explanation of the list syntax.
<!--l. 333--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese28.html" >next</a>] [<a
href="nipguidese25.html" >prev</a>] [<a
href="nipguidese25.html#tailnipguidese25.html" >prev-tail</a>] [<a
href="nipguidese26.html" >front</a>] [<a
href="nipguidech6.html#nipguidese26.html" >up</a>] </p></div>
<!--l. 333--><p class="indent" > <a
id="tailnipguidese26.html"></a>
</body></html>
|