-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconditionals.html
165 lines (164 loc) · 6.14 KB
/
conditionals.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<html dir="ltr">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Arc: Conditionals</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="iteration.html">Iteration</a>
Up: <a href="index.html">Contents</a>
Next: <a href="evaluation.html">Evaluation</a>
</div>
<h1 class="links">Conditionals</h1>
The fundamental conditional operator in Arc is <code>if</code>. It is similar to the Lisp <code>if</code>, except it eliminates the parentheses around the clauses. For a simple conditional with multiple body statements, <code>when</code> or its opposite <code>unless</code> can be used.
<p>
Arc provides several conditionals that assign the test expression to a variable, similar to <code>let</code>. The <code>iflet</code>, <code>caselet</code>, and <code>whenlet</code> macros are useful if the test expression is used inside the body.
<h2>Conditionals</h2>
<p><table class='arc'>
<tr>
<td class='arc'><a name='if'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>if</span> <span class='args'>[test expr] ... [else-expr]</span>
<div class='desc'>Arc is the basic conditional operation.
It takes a sequence of tests and expressions. The expression corresponding
to the first true test is returned. Other expressions are not evaluated.</div>
</td>
<td class='arc'><pre>
>(if nil "Nil is true"
0 "0 is true"
"What is true?")
<span class="return">"0 is true"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='iflet'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>iflet</span> <span class='args'>var expr then-expr [test1 expr1] ... [else-expr]</span>
<div class='desc'>Evaluates <code>expr</code>. If true, <code>expr</code> is assigned to <code>var</code> and <code>then-expr</code> is evaluated and returned. Otherwise, the remaining arguments are processed as normal <code>if</code> clauses.</div>
</td>
<td class='arc'><pre>
>(iflet x 42 (+ x 1))
<span class="return">43
</span></pre>
<pre>
>(iflet x nil (+ x 1))
<span class="return">nil
</span></pre>
<pre>
>(iflet x nil (+ x 1)
(< 1 2) 55)
<span class="return">55
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='when'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>when</span> <span class='args'>test [body ...]</span>
<div class='desc'>Executes body if test is true. This is similar to <code>if</code>, except it allows multiple body statements but only has a single test clause. </div>
</td>
<td class='arc'><pre>
>(when 1 (pr "a") (pr "b"))
<span class="stdout">ab
</span><span class="return">"b"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='whenlet'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>whenlet</span> <span class='args'>var expr [body ...]</span>
<div class='desc'>Evaluates <code>expr</code>. If true, the value is assigned to <code>var</code> and <code>body</code> is executed.</div>
</td>
<td class='arc'><pre>
>(whenlet x nil (prn "hi") (+ x 1))
<span class="return">nil
</span></pre>
<pre>
>(whenlet x 1 (prn "hi") (+ x 1))
<span class="stdout">hi
</span><span class="return">2
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='unless'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>unless</span> <span class='args'>test [body ...]</span>
<div class='desc'>Executes body if test is false. This is the opposite of 'when'.</div>
</td>
<td class='arc'><pre>
>(unless 1 (pr "a") (pr "b"))
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='case'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>case</span> <span class='args'>arg [test1 expr1] ... [else-expr]</span>
<div class='desc'><code>arg</code> is evaluated. It is then compared to the test values in sequence. If it matches one, the corresponding expr is evaluated and returned. If there is no match and no <code>else-expr</code>, <code>nil</code> is returned.</div>
</td>
<td class='arc'><pre>
>(case 'b
a 1
b 2
3)
<span class="return">2
</span></pre>
<pre>
>(case 42
10 "foo"
42 "bar")
<span class="return">"bar"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='caselet'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>caselet</span> <span class='args'>var arg [test1 expr1] ... [else-expr]</span>
<div class='desc'><code>arg</code> is evaluated and assigned to <code>var</code>. It is then compared to the test values in sequence. If it matches one, the corresponding expr is evaluated and returned. If there is no match and no <code>else-expr</code>, <code>nil</code> is returned.</div>
</td>
<td class='arc'><pre>
>(caselet x 'b
a 1
b 2
3)
<span class="return">2
</span></pre>
<pre>
>(caselet x 42
10 (+ x 3)
42 (+ x 5))
<span class="return">47
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='check'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>check</span> <span class='args'>expr test [alt]</span>
<div class='desc'>Evaluates <code>expr</code> and applies predicate <code>test</code>. If true, returns the evaluated value. Otherwise returns <code>alt</code> or nil. <code>alt</code> can be a function to try again, even calling <code>check</code> recursively.</div>
</td>
<td class='arc'><pre>
>(check (+ 10 10) odd "foo")
<span class="return">"foo"
</span></pre>
<pre>
>(check (+ 10 10) even "bar")
<span class="return">20
</span></pre>
</td></tr>
</table>
<p>
Copyright 2008 Ken Shirriff.
</body>
</html>