-
Notifications
You must be signed in to change notification settings - Fork 6
/
REPL.html
45 lines (45 loc) · 3.79 KB
/
REPL.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<link rel="stylesheet" href="Class.css" type="text/css" />
</head>
<body>
<h1 id="section"></h1>
<h1 id="repl---read-evaluate-print-loop.">REPL - Read, Evaluate, Print, Loop.</h1>
<p>A REPL is a loop (the L part) that + Reads input and parses it into an R command(s) + Evaluates the command(s) + Prints the result and starts over again.</p>
<p>Why do we care? Partially because it helps to understand the nature of an error which might be a syntax error (at parse time) or run/evaluation-time semantic errors (e.g. no such variable, wrong input to a function).</p>
<p>We start an interpreter (R, python, MATLAB, etc.) and it gives us a prompt, waiting for our command. It is awaiting and reading our input. When we enter a character, it processes it potentially doing something such as suggesting auto-completion. The R part in the REPL comes in when we hit the <strong>return</strong> key. The REPL attempts to <strong>parse</strong> the text. There are three possibilities: 1) the line makes complete sense as a command, 2) contains a syntax error, or 3) forms an incomplete command that makes partial sense and that needs to be completed.</p>
<p>There are two steps in an R command - checking it is syntactically correct, and then if it makes sense - evaluating it.</p>
<p>R <strong>parses</strong> the text of the command to see if it is syntactically correct. If it is not, we get a syntax error. There are so many ways to write a command that is not syntactically correct. For example,</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">lapply</span>(x mean)
2pi =<span class="st"> </span><span class="dv">1</span>
x[[<span class="dv">2</span>][<span class="dv">1</span>]
<span class="kw">lapply</span>(x mean <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)</code></pre>
<p>If the command is syntatically complete and legitimate, i.e., makes sense syntactically, R tries to evaluate the <strong>expression</strong>. The rules for evaluation are somewhat involved, but basically we find variables along the <a href="SearchPath.html">search path</a>, and we make function calls. That's pretty much all there is to the R language.</p>
<p>It may seem that there is a lot more to the language, e.g. math operators, logic operators, sequence operator (a:b), subsetting ([, [[, $), assignments, for and while loops, if statements. But in fact, these are all function calls. That is one of the elegant aspects of the R language. Master function calls and you master the language. However, function calls are rich.</p>
</body>
</html>