-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanaphoric.html
111 lines (109 loc) · 4.87 KB
/
anaphoric.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
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
105
106
107
108
109
110
111
<html dir="ltr">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Arc: Anaphoric operations</title>
<link rel="shortcut icon" href="/assets/favicon.png">
<link rel="stylesheet" type="text/css" href="code.css">
<link href="/assets/bootstrap.css" rel="stylesheet">
</head>
<body style='margin:12px 50px 0'>
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<ul class="nav navbar-nav">
<li><a href="/ref/">Arc 3.1</a>
<li><a href="http://tryarc.org">Try it</a></li>
<li><a href="http://github.com/arclanguage/anarki">Get it</a></li>
<li><a href="http://ycombinator.com/arc/tut.txt">Tutorial</a></li>
<li><a href="http://arclanguage.org/forum">Forum</a></li>
</ul>
</div>
</div> <!-- end of navbar -->
<div class="links">Previous: <a href="error.html">Error handling</a>
Up: <a href="index.html">Contents</a>
Next: <a href="html.html">HTML generation</a>
</div>
<h1 class="links">Anaphoric operations</h1>
Anaphoric operations provide a mechanism to refer back to themselves.
This can be useful when an expression needs to be used later, as in <code>aif</code>, <code>awhen</code>, or <code>aand</code>. Anaphoric functions allow a function to refer back to itself recursively, without creating a named function.
<p>See <a href='http://www.bookshelf.jp/texi/onlisp/onlisp_15.html'>On Lisp Chapter 14</a> for a detailed explanation of anaphoric operations.
<h2>Anaphoric operations</h2>
<p><table class='arc'>
<tr>
<td class='arc'><a name='aif'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>aif</span> <span class='args'>expr body [expr body] ...</span>
<div class='desc'>Anaphoric <code>if</code>: each <code>expr</code> is evaluated until one is true, and then the corresponding <code>body</code> is executed. Within the body, the anaphoric variable <code>it</code> refers back to the value of <code>expr</code>.</div>
</td>
<td class='arc'><pre>
>(aif (> 1 2) (+ it 1) 42 (+ it 2))
<span class="return">44
</span></pre>
<pre>
>(aif nil (+ it 1))
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='awhen'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>awhen</span> <span class='args'>expr [body ...]</span>
<div class='desc'>Anaphoric <code>when</code>: if the <code>expr</code> is true, the <code>body</code> is executed. Within the body, the variable <code>it</code> refers back to the value of <code>expr</code>.</div>
</td>
<td class='arc'><pre>
>(awhen (* 2 3) (+ it 1))
<span class="return">7
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='aand'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>aand</span> <span class='args'>[arg ...]</span>
<div class='desc'>Anaphoric <code>and</code>. Returns the last argument if all arguments are true, otherwise returns <code>nil</code>. Inside each argument the anaphoric variable <code>it</code> refers to the value of the previous argument. Like <code>and</code>, lazy evaluation is used, so evaluation stops after encountering a false argument.</div>
</td>
<td class='arc'><pre>
>(aand 1 (+ it 2) (* it 10))
<span class="return">30
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='afn'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>afn</span> <span class='args'>parms [body ...]</span>
<div class='desc'>Creates an anaphoric function, which can be called recursively with the name <code>self</code>. This allows a recursive function to be created without assigning it a name.</div>
</td>
<td class='arc'><pre>
>((afn (x) (if (is x 0) 1 (* 2 (self (- x 1))))) 5)
<span class="return">32
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='rfn'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>rfn</span> <span class='args'>name parms [body ...]</span>
<div class='desc'>Creates a function with the given <code>name</code>. The name is only inside the scope of the <code>rfn</code> macro. This allows recursive functions to be created without polluting the wider scope.</div>
</td>
<td class='arc'><pre>
>((rfn pow2 (x) (if (is x 0) 1 (* 2 (pow2 (- x 1))))) 5)
<span class="return">32
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='trav'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>trav</span> <span class='args'>obj [function ...]</span>
<div class='desc'>Recursive traversal. Applies each <code>function</code> in sequence to <code>obj</code>, if <code>obj</code> is not <code>nil</code>. The function can recursively call itself with a new <code>obj</code> with <code>(self obj)</code>.</div>
</td>
<td class='arc'><pre>
>(trav '(1 2 3 4) (fn (_) (prn _)) (fn (_) (self (cdr _))))
<span class="stdout">(1 2 3 4)
(2 3 4)
(3 4)
(4)
</span><span class="return">nil
</span></pre>
</td></tr>
</table>
<p>
Copyright 2008 Ken Shirriff.
</body>
</html>