File: part7.html

package info (click to toggle)
nyquist 3.24%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,156 kB
  • sloc: ansic: 74,757; lisp: 18,169; java: 10,942; cpp: 6,688; sh: 175; xml: 58; makefile: 40; python: 15
file content (741 lines) | stat: -rw-r--r-- 34,962 bytes parent folder | download | duplicates (2)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
<!DOCTYPE html>
<html><head><title>SAL</title>
<link rel="stylesheet" type="text/css" href="nyquiststyle.css">
<link rel="icon" href="nyquist-icon.png" />
<link rel="shortcut icon" href="nyquist-icon.png" />
</head>
<body bgcolor="ffffff">
<a href = "part6.html">Previous Section</a> | <a href = "part8.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a>
<hr>
<a name = "51"><h2>SAL</h2></a>
<p>Nyquist supports two languages: XLISP and SAL. In some sense, XLISP and SAL
are the same language, but with differing syntax. This chapter describes SAL: how it works, SAL syntax and semantics, and the relationship between SAL and XLISP, and differences between Nyquist SAL and Common Music SAL.</p>

<p>Nyquist SAL is based on Rick Taube's SAL language, which is
part of Common Music. SAL offers the power
of Lisp but features a simple, Algol-like syntax. SAL is implemented
in Lisp: Lisp code translates SAL into a Lisp program and uses the
underlying Lisp engine to evaluate the program. Aside from the translation
time, which is quite fast, SAL programs execute at about the same speed as
the corresponding Lisp program. (Nyquist SAL programs run just
 slightly slower than XLISP
because of some runtime debugging support automatically added to
user programs by the SAL compiler.)</p>

<p>From the user's perspective, these implementation details are hidden. You 
can enter SAL mode from XLISP by typing <code>(SAL)</code> to the XLISP prompt.
The SAL input prompt (<code>SAL&gt; </code>) will be displayed. From that point on,
you simply type SAL commands, and they will be executed. By setting a 
preference in the NyquistIDE program, SAL mode will be entered automatically.</p>

<p>It is possible to encounter errors that will take you from the SAL interpreter
to an XLISP prompt. In general, the way to get back to SAL is by typing 
<code>(top)</code> to get back to the top level XLISP interpreter and reset the
Nyquist environment. Then type <code>(sal)</code> to restart the SAL interpreter.</p>
<a name = "52"><h3>SAL Syntax and Semantics</h3></a><a name="index183"></a>
<p> The most unusual feature of SAL syntax is that identifiers
are Lisp-like, including names such as &ldquo;play-file&rdquo; and even &ldquo;*warp*.&rdquo;
In SAL, most operators must be separated from identifiers by white space.
For example, <code>play-file</code> is one identifier, but <code>play - file</code>
is an expression for &ldquo;play minus file,&rdquo; where <code>play</code> and <code>file</code> are
two separate identifiers. Fortunately, no spaces are needed around commas
and parentheses.</p>

<p>The set SAL identifiers is difficult to describe due to a number of interacting
rules designed to prevent surprising or confusing code. The exact details can be
found in the <code>symbol-token?</code> function in <code>sal-parse.lsp</code>. To determine
if an identifier is allowed, you can also follow the following example, which tests
whether <code>$a@</code> is a valid identifier:</p>

<p></p>
<pre>
SAL&gt; print type-of(quote($a@))
</pre>

<p></p>

<p>If this statement prints <code>SYMBOL</code>, then the quoted expression is a valid identifier. If SAL reports a "parse error" or some other type, then the quoted expression is not a valid identifier.</p>

<p>In SAL, whitespace (any sequence of space, newline, or tab characters)
is sometimes necessary to separate lexical tokens, but
otherwise, spaces and indentation are ignored. To make SAL readable,
it is <i>strongly</i> advised that you indent SAL programs as in the examples
here. The NyquistIDE program is purposely insistent about SAL indentation,
so if you use it to edit SAL programs, your indentation should be 
both beautiful and consistent.</p>

<p>As in Lisp (but very unlike C or Java), comments <a name="index184"></a>
are indicated by
semicolons. Any text from an unquoted semicolon to the end of the 
line is ignored.</p>

<p></p>
<pre>
<i>; this is a comment</i>
<i>; comments are ignored by the compiler</i>
print "Hello World" <i>; this is a SAL statement</i>
</pre>

<p></p>

<p>As in Lisp, identifiers are translated to upper-case, making SAL 
case-insensitive<a name="index185"></a>. For example, the function name <code>autonorm</code> can
be typed in lower case or as <code>AUTONORM</code>, <code>AutoNorm</code>, or even 
<code>AuToNoRm</code>. All forms denote the same function. The recommended
approach is to write programs in all lower case.</p>

<p>SAL is organized around statements, most of which
contain expressions. We will begin with expressions and then look at
statements.</p>
<a name = "53"><h4>Expressions</h4></a><a name="index186"></a><a name="index187"></a><a name = "54"><h5>Simple Expressions</h5></a>
<p>As in XLISP, simple expressions include:
<ul>
<li>
integers (FIXNUM's), such as <code>1215</code>, </li>
<li>floats (FLONUM's) such as <code>12.15</code>, </li>
<li>strings (STRING's) such as <code>"Magna Carta"</code>, and</li>
<li>symbols (SYMBOL's) such as <code>magna-carta</code>. A symbol with a leading colon 
(<code>:</code>) evaluates to itself as in Lisp. Otherwise, a symbol denotes either
a local variable, a formal parameter, or a global variable. As in Lisp, 
variables do not have data types or type declarations. The type of a 
variable is determined at runtime by its value.
</li></ul></p>

<p>Additional simple expressions in SAL are:
<ul>
<li>
lists such as <code>{c 60 e 64}</code>. Note that there are no commas to separate list elements, and symbols in lists are not evaluated as variables but stand for themselves. Lists may contain numbers, booleans (which represent XLISP's <code>T</code> or <code>NIL</code>, SAL identifiers (representing XLISP symbols), strings, SAL operators (representing XLISP symbols), and nested lists.</li>
<li>Booleans: SAL interprets <code>#t</code><a name="index188"></a> as true and <code>#f</code><a name="index189"></a>
as false. (As far
as the SAL compiler is concerned, <code>t</code> and <code>nil</code><a name="index190"></a> are just variables.
Since these are the Lisp versions of true and false, they are interchangeable
with <code>#t</code> and <code>#f</code>, respectively.)
</li></ul>
A curious property of Lisp and Sal is that <i>false</i> and the empty list (<code>nil</code>) are 
the same value. Since SAL is based on Lisp, <code>#f</code> and <code>{}</code> (the empty
list)<a name="index191"></a> and <code>nil</code> are all equal.</p>
<a name = "55"><h5>Operators</h5></a>
<p>Expressions can be formed with unary and binary operators using infix notation. The operators are:
<dl>
<dt>
<a name="index192"></a><code>+</code></dt>
<dd>addition, including sounds</p>
<dt><a name="index193"></a><code>-</code></dt>
<dd>subtraction, including sounds<br><br>
<dt><a name="index194"></a><code>*</code></dt>
<dd>multiplication, including sounds<br><br>
<dt><a name="index195"></a><code>/</code></dt>
<dd>division (due to divide-by-zero problems, does not operate on sounds)<br><br>
<dt><a name="index196"></a><code>%</code></dt>
<dd>modulus (remainder after division)<br><br>
<dt><a name="index197"></a><code>^</code> </dt>
<dd>exponentiation<br><br>
<dt><a name="index198"></a><code>=</code></dt>
<dd>equal (using Lisp <code>equal</code> for non-lists, and
comparing lists element-by-element recursively) <a href = "foot.html#foot1">(Footnote 1)</a> <!DOCTYPE html>
<html><head><title>Footnotes</title>
<link rel="stylesheet" type="text/css" href="nyquiststyle.css">
<link rel="icon" href="nyquist-icon.png" />
<link rel="shortcut icon" href="nyquist-icon.png" />
</head>
<br><br>
<dt><a name="index199"></a><code>!=</code></dt>
<dd>not equal<br><br>
<dt><a name="index200"></a><code>&gt;</code></dt>
<dd>greater than<br><br>
<dt><a name="index201"></a><code>&lt;</code></dt>
<dd>less than<br><br>
<dt><a name="index202"></a><code>&gt;=</code></dt>
<dd>greater than or equal<br><br>
<dt><a name="index203"></a><code>&lt;=</code></dt>
<dd>less than or equal<br><br>
<dt><a name="index204"></a><code>~=</code></dt>
<dd>approximately equal. Numbers are approximately
equal if they are within <code>*~=tolerance*</code> of each
other. <code>*~=tolerance*</code> is initially 0.000001. Non-numbers are
compared with the XLISP <code>equal</code> function, and lists are compared
element-by-element (recursively) using <code>~=</code>. <a href = "foot.html#foot2">(Footnote 2)</a> <br><br>
<dt><a name="index205"></a><code>&amp;</code></dt>
<dd>logical and<br><br>
<dt><a name="index206"></a><code>|</code></dt>
<dd>logical or<br><br>
<dt><a name="index207"></a><code>!</code></dt>
<dd>logical not (unary)<br><br>
<dt><a name="index208"></a><a name="index209"></a><a name="index210"></a><code>@</code></dt>
<dd>time shift<br><br>
<dt><a name="index211"></a><a name="index212"></a><a name="index213"></a><code>@@</code></dt>
<dd>time shift to absolute time<br><br>
<dt><a name="index214"></a><a name="index215"></a><code>~</code></dt>
<dd>time stretch<br><br>
<dt><a name="index216"></a><a name="index217"></a><code>~~</code></dt>
<dd>time stretch to absolute stretch factor
</dd></dl>
Again, remember that operators <i>must</i> be delimited from their operands using
spaces or parentheses. Operator precedence is based on the following levels of 
precedence:

<p></p>
<pre>
@ @@ ~ ~~
^
/ * 
% - +
~= &lt;= &gt;= &gt; ~= =
!
&amp;
|
</pre>

<p></p>
<a name = "56"><h5>Function Calls</h5></a><a name="index218"></a>
<p>A function call is a function name followed by zero or more comma-delimited
argument expressions
enclosed within parentheses:
</p>
<pre>
list()
piano-note(2.0, c4 + interval, 100)
</pre>

<p>
Some functions use named parameters,
in which case the name of the argument with a colon precedes the argument
expression.
</p>
<pre>
s-save(my-snd(), ny:all, "tmp.wav", play: #t, bits: 16)
</pre>

<p></p>
<a name = "57"><h5>Array Notation</h5></a><a name="index219"></a>
<p>An array reference is a variable identifier followed by an index expression
in square brackets, e.g.:
</p>
<pre>
x[23] + y[i]
</pre>

<p></p>
<a name = "58"><h5>Conditional Values</h5></a><a name="index220"></a><a name="index221"></a>
<p>The special operator <code>#?</code> evaluates the first argument expression. 
If the result is <i>true</i>, the second expression is evaluated and
its value is returned. If <i>false</i>, the third expression is evaluated
and returned (or <i>false</i> is returned if there is no third expression):
</p>
<pre>
#?(random(2) = 0, unison, major-third)
#?(pitch &gt;= c4, pitch - c4) <i>; returns false if pitch &lt; c4</i>
</pre>

<p></p>
<a name = "59"><h4>SAL Statements</h4></a><a name="index222"></a>
<p>SAL compiles and evaluates <i>statements</i> one at a time. You can type
statements at the SAL prompt or load a file containing SAL statements.
SAL statements are described below. The syntax is indicated at the
beginning of each statement type description: <code>this font</code> indicates
literal terms such as keywords, <i>the italic font</i> indicates a 
place-holder for some other statement or expression. Bracket [like this]
indicate optional (zero or one) syntax elements, while braces with a plus
{like this}+ indicate one or more occurrences of a syntax element. Braces
with a star {like this}* indicate zero or more occurrences of a syntax element: { <i>non-terminal</i> }* is equivalent to [ {<i>non-terminal</i>}+ ].</p>
<a name = "60"><h5>begin and end</h5></a><a name="index223"></a><a name="index224"></a><code>begin</code> [<i>with-stmt</i>] {<i>statement</i>}+ <code>end</code>
<p>A <code>begin</code>-<code>end</code> statement 
consists of a sequence of statements surrounded by
the <code>begin</code> and <code>end</code> keywords. This form is often used for function
definitions and after <code>then</code> or <code>else</code> where the syntax demands a
single statement but you want to perform more than one action. Variables may be
declared using an optional <code>with</code> statement immediately after <code>begin</code>.
For example:
</p>
<pre>
begin
  with db = 12.0,
       linear = db-to-linear(db)
  print db, "dB represents a factor of", linear
  set scale-factor = linear
end  
</pre>

<p></p>
<a name = "61"><h5>chdir</h5></a><a name="index225"></a><code>chdir</code> <i>expression</i>
<p>The <code>chdir</code> statement changes the working directory. This statement
is provided for compatibility with Common Music SAL, but it really
should be avoided if you use NyquistIDE. The <i>expression</i> following the
<code>chdir</code> keyword should evaluate to a string that is a directory
path name. Note that literal strings themselves are valid expressions.
</p>
<pre>
chdir "/Users/rbd/tmp"
</pre>

<p></p>
<a name = "62"><h5>define variable</h5></a><a name="index226"></a><a name="index227"></a>
<p>[<code>define</code>] <code>variable</code> <i>name</i> [= <i>expression</i>] {, <i>name</i> [= <i>expression</i>]}*</p>

<p>Global variables can be declared and initialized. A list of variable names,
each with an optional initialization follows the <code>define variable</code> 
keywords. (Since <code>variable</code> is a keyword, <code>define</code> is redundant
and optional in Nyquist SAL, but required in Common Music SAL.)
If the initialization part is omitted, the variable is initialized
to false. Global variables do not really need to be declared: just using the
name implicitly creates the corresponding variable. However, it is an error
to use a global variable that has not been initialized;
<code>define variable</code> is a good way to introduce a variable (or constant)
with an initial value into your program.</p>

<p></p>
<pre>
define variable transposition = 2,
                print-debugging-info, <i>; initially false</i>
                output-file-name = "salmon.wav"
</pre>

<p></p>
<a name = "63"><h5>define function</h5></a><a name="index228"></a><a name="index229"></a>
<p>[<code>define</code>] <code>function</code> <i>name</i> <code>(</code> [<i>parameter</i>] {, <i>parameter</i>}* <code>)</code> <i>statement</i></p>

<p>Before a function be called from an expression (as described above), it must
be defined. A function definition gives the function <i>name</i>, a list of
<i>parameters</i>, and a <i>statement</i>. When a function is called, the actual
parameter expressions are evaluated from left to right and the formal parameters
of the function definition are set to these values. Then, <i>statement</i> is 
evaluated. </p>

<p>The formal parameters may be positional parameters that are matched with
actual parameters by position from left to right. Syntactically, these are
symbols and these symbols
are essentially local variables that exist only until <i>statement</i> completes 
or a <code>return</code> statement causes the function evaluation to end. As in Lisp,
parameters are passed by value, so assigning a new value to a formal parameter
has no effect on the actual value. However, lists and arrays are not copied,
so internal changes to a list or array produce observable side effects.</p>

<p>Alternatively, formal parameters may be keyword parameters. Here the <i>parameter</i>
is actually a pair: a keyword parameter, which is a symbol followed by a colon,
and a default value, given by any expression. Within the body of the function,
the keyword parameter is named by a symbol whose name matches the keyword
parameter except there is no final colon.
</p>
<pre>
define function foo(x: 1, y: bar(2, 3))
    display "foo", x, y

exec foo(x: 6, y: 7)
</pre>

<p>
In this example, <code>x</code> is bound to the value 6 and <code>y</code> is bound to
the value 7, so the example prints &ldquo;<code>foo : X = 6, Y = 7</code>&rdquo;. Note that
while the keyword parameters are <code>x:</code> and <code>y:</code>, the corresponding
variable names in the function body are <code>x</code> and <code>y</code>, respectively.</p>

<p>The <i>parameters</i> are meaningful only within the lexical (static) scope of
<i>statement</i>. They are not accessible from within other
functions even if they are called by this function.</p>

<p>Use a <code>begin</code>-<code>end</code> statement if the body of the function should
contain more than one statement or you need to define local variables. Use 
a <code>return</code> statement to return a value from the function. If <i>statement</i>
completes without a <code>return</code>, the value false is returned.</p>
<a name = "64"><h5>exec</h5></a><a name="index230"></a><code>exec</code> <i>expression</i>
<p>Unlike most other programming languages, you cannot simply type an expression as
a statement. If you want to evaluate an expression, e.g. call a function, 
you must use an <code>exec</code> statement. The statement simply evaluates 
the <i>expression</i>. For example,
</p>
<pre>
exec set-sound-srate(22050.0) <i>; change default sample rate</i>
</pre>

<p></p>
<a name = "65"><h5>if</h5></a><a name="index231"></a><code>if</code> <i>test-expr</i> <code>then</code> <i>true-stmt</i> [<code>else</code> <i>false-stmt</i>]
<p>An <code>if</code> statement evaluates the expression <i>test-expr</i>. If it is true,
it evaluates the statement <i>true-stmt</i>. If false, the statement
<i>false-stmt</i> is evaluated. Use a <code>begin</code>-<code>end</code> statement
to evaluate more than one statement in then <code>then</code> or <code>else</code>
parts.</p>

<p></p>
<pre>
if x &lt; 0 then x = -x <i>; x gets its absoute value</i>

if x &gt; upper-bound then
  begin
    print "x too big, setting to", upper-bound
    x = upper-bound
  end
else
  if x &lt; lower-bound then
    begin
      print "x too small, setting to", lower-bound
      x = lower-bound
    end
</pre>

<p>
Notice in this example that the <code>else</code> part is another <code>if</code>
statement. An <code>if</code> may also be the <code>then</code> part of another
<code>if</code>, so there could be two possible <code>if</code>'s with which to
associate an <code>else</code>. An <code>else</code> clause always associates
with the closest previous <code>if</code> that does not already have an
<code>else</code> clause.</p>
<a name = "66"><h5>when</h5></a><code>when</code> <i>test</i> <i>statement</i>
<p>The <code>when</code> statement is similar to <code>if</code>, but there is no <code>else</code> clause.</p>

<p></p>
<pre>
when *debug-flag* print "you are here"
</pre>

<p></p>
<a name = "67"><h5>unless</h5></a><code>unless</code> <i>test</i> <i>statement</i>
<p>The <code>unless</code> statement is similar to <code>when</code> (and <code>if</code>) but the
<i>statement</i> is executed when the <i>test</i> expression is <i>false</i>.</p>

<p></p>
<pre>
unless count = 0 set average = sum / count
</pre>

<p></p>
<a name = "68"><h5>load</h5></a><a name="index232"></a><code>load</code> <i>path-expression</i>
<p>The <code>load</code> command loads a file named by <i>path-expression</i>, which must
be either a literal string or a variable name. When evauated, 
<i>path-expression</i> must result in a string path name for the 
file. The extension <code>.sal</code> is appended to the path if needed.
To load a file, SAL interprets
each statement in the file, stopping when the end of the file or an error
is encountered. If the file name ends in the extension <code>.lsp</code>, the
file is assumed to 
contain Lisp expressions, which are evaluated by the XLISP interpreter.
In general, SAL files should end with the extension <code>.sal</code>.</p>
<a name = "69"><h5>loop</h5></a><a name="index233"></a><code>loop</code> [<i>with-stmt</i>] {<i>stepping</i>}* {<i>stopping</i>}* {<i>action</i>}+ [<i>finally</i>] <code>end</code>
<p>The <code>loop</code> statement is by far the most complex statement in SAL, but
it offers great flexibility for just about any kind of iteration. The basic
function of a loop is to repeatedly evaluate a sequence of <i>action</i>'s which 
are statements. Before the loop begins, local variables may be declared in 
<i>with-stmt</i>, a <code>with</code> statement. </p>

<p>The <i>stepping</i> clauses do several
things. They introduce and initialize additional local variables similar
to the <i>with-stmt</i>.
However, these local variables are updated to new values after the <i>action</i>'s.
In addition, some <i>stepping</i> clauses have associated stopping conditions, 
which are tested on each iteration <i>before</i> evaluating the <i>action</i>'s.</p>

<p>There are also <i>stopping</i> clauses that provide additional tests to
stop the iteration. These are also evaluated and tested
on each iteration before evaluating the <i>action</i>'s.</p>

<p>When some <i>stepping</i> or <i>stopping</i> condition causes the iteration to stop,
the <i>finally</i> clause is evaluated (if present). Local variables and their
values can still be accessed in the <i>finally</i> clause. After the <i>finally</i>
clause, the <code>loop</code> statement completes.</p>

<p>The <i>stepping</i> clauses are the following:
<dl>
<dt>
<code>repeat</code> <i>expression</i></dt>
<dd>Sets the number of iterations to the value of <i>expression</i>, which should be an integer (FIXNUM).</p>
<dt><code>for</code> <i>var</i> = <i>expression</i> [ <code>then</code> <i>expr2</i> ]</dt>
<dd>Introduces a new local
variable named <i>var</i> and initializes it to <i>expression</i>. Before each subsequent
iteration, <i>var</i> is set to the value of <i>expr2</i>. If the <code>then</code> part is
omitted, <i>expression</i> is re-evaluated and assigned to <i>var</i>
on each subsequent iteration. Note that this differs from a <i>with-stmt</i> where
expressions are evaluated and variables are only assigned their values once. <b>Warning:</b> All <code>for</code> clauses evaluate <i>before</i> stopping conditions are evaluated, which means if the loop iterates N times, the <code>for</code> clauses run N+1 times. To avoid evaluation after the last iteration, you can often use <code>set</code> <i>var</i> = <i>expr2</i>.<br><br>
<dt><code>for</code> <i>var</i> <code>in</code> <i>expression</i></dt>
<dd>Evaluates <i>expression</i> to 
obtain a list and creates a new local variable initialized to the first element
of the list. After each iteration, <i>var</i> is assigned the next element of the
list. Iteration stops when <i>var</i> has assumed all values from the list. If the
list is initially empty, the loop <i>action</i>'s are not evaluated (there are zero
iterations).<br><br>
<dt><code>for</code> <i>var</i> [<code>from</code> <i>from-expr</i>] [[<code>to</code> | <code>below</code> | <code>downto</code> | <code>above</code>] <i>to-expr</i>] [<code>by</code> <i>step-expr</i>]</dt>
<dd>Introduces a new local variable named <i>var</i> and intialized
to the value of the expression <i>from-expr</i> (with a default value of 0). After
each iteration of the loop, <i>var</i> is incremented by the value 
of <i>step-expr</i> (with a default value of 1). 
The iteration ends when <i>var</i> is greater than 
the value of <i>to-expr</i> if there is a <code>to</code> clause, 
greater than or equal to the value of <i>to-expr</i> 
if there is a <code>below</code> clause, 
less than the value of <i>to-expr</i> if there is a <code>downto</code> clause, 
or less than or equal to the value of <i>to-expr</i> if there is a <code>above</code>
clause. (In the cases of <i>downto</i> and <i>above</i>, the default increment value
is -1. If there
is no <code>to</code>, <code>below</code>, <code>downto</code>, or <code>above</code> clause, no iteration stop test is created for this
stepping clause.
</dd></dl>
<p>The <i>stopping</i> clauses are the following:
<dl>
<dt>
<code>while</code> <i>expression</i></dt>
<dd>The iterations are stopped when <i>expression</i> evaluates to <i>false</i>. Anything not false is considered to mean true.</p>
<dt><code>until</code> <i>expression</i></dt>
<dd>The iterations are stopped when <i>expression</i> evaluates to <i>true</i>.
</dd></dl>
<p>The <i>finally</i> clause is defined as follows:
<a name="index234"></a>
<dl>
<dt>
<code>finally</code> <i>statement</i></dt>
<dd>The <i>statement</i> is evaluated when one of the 
<i>stepping</i> or <i>stopping</i> clauses ends the loop. As always, <i>statement</i> may
be a <code>begin</code>-<code>end</code> statement. If an <i>action</i> evaluates a <code>return</code>
statement, the <code>finally</code> statement is not executed.
</dd></dl></p>
<a name="index235"></a>Loops often fall into common patterns, such as iterating a fixed number of
times, performing an operation on some range of integers, collecting results
in a list, and linearly searching for a solution. These forms are illustrated
in the examples below.
<p></p>
<pre>
<i>; iterate 10 times</i>
loop
  repeat 10
  print random(100)
end
</pre>

<p>
</p>
<pre>
<i>; print even numbers from 10 to 20
; note that 20 is printed. On the next iteration,
;   i = 22, so i &gt;= 22, so the loop exits.</i>
loop
  for i from 10 below 22 by 2
  print i
end
</pre>

<p>
</p>
<pre>
<i>; collect even numbers in a list</i>
loop
  with lis
  for i from 0 to 10 by 2
  set lis @= i <i>; push integers on front of list,</i>
               <i>; which is much faster than append,</i>
               <i>; but list is built in reverse</i>
  finally set result = reverse(lis)
end
<i>; since "to" is used, the loop stops when i &gt; 10</i>
<i>; now, the variable result has a list of evens:</i> {0 2 4 6 8 10}
</pre>

<p>
</p>
<pre>
<i>; find the first even number in a list</i>
result = #f <i>; #f means "false"</i>
loop
  for elem in lis
  until evenp(elem)
  finally result = elem
end
<i>; result has first even value in lis (or it is #f)</i>
</pre>

<p></p>
<a name = "70"><h5>play</h5></a><a name="index236"></a><code>play</code> <i>expr</i>
<p>The <code>play</code> statement plays the sound denoted by <i>expr</i>, an expression.</p>
<a name = "71"><h5>plot</h5></a><a name="index237"></a><a name="index238"></a><code>plot</code> <i>expr</i> {, <i>dur</i>, <i>n</i>}
<p>The <code>plot</code> statement plots the sound denoted by <i>expr</i>, an
expression. If you plot a long sound, the <code>plot</code> statement will
by default truncate the sound to 2.0 seconds and resample the signal
to 1000 points. The optional <i>dur</i> is an expression that specifies
the (maximum) duration to be plotted, and the optional <i>n</i> specifies
the number of points to be plotted. Executing a <code>plot</code> statement
is equivalent to calling the <code>s-plot</code> function (see Section
<a href = "part8.html#100">Sound File Input and Output</a>).</p>
<a name = "72"><h5>print</h5></a><a name="index239"></a><a name="index240"></a><code>print</code> <i>expr</i> {, <i>expr</i>}*
<p>The <code>print</code> statement prints the values separated by
spaces and followed by a newline. [Note that in the original
SAL, the newline is printed <i>before</i> the values, not after.]</p>
<a name = "73"><h5>display</h5></a><a name="index241"></a><a name="index242"></a><code>display</code> <i>string</i> {, <i>expression</i>}*
<p>The <code>display</code> statement is handy for debugging. At present, it is only
implemented in Nyquist SAL. When executed, <code>display</code> prints the <i>string</i>
followed by a colon and then, for each <i>expression</i>, the expression and its
value are printed; after the last expression, a newline is printed. For example,
</p>
<pre>
display "In function foo", bar, baz
</pre>

<p>
prints
</p>
<pre>
In function foo : bar = 23, baz = 5.3
</pre>

<p>
SAL may print the expressions using Lisp syntax, e.g. if the expression is
&ldquo;bar + baz,&rdquo; do not be surprised if the output is &ldquo;<code>(sum bar baz) = 28.3</code>.&rdquo;</p>

<p></p>
<pre>
print "The value of x is", x
</pre>

<p></p>
<a name = "74"><h5>return</h5></a><a name="index243"></a><code>return</code> <i>expression</i>
<p>The <code>return</code> statement can only be used inside a function. It evaluates
<i>expression</i> and then the function returns the value of the expression
to its caller.</p>
<a name = "75"><h5>set</h5></a><a name="index244"></a><code>set</code> <i>var</i> <i>op</i> <i>expression</i> {, <i>var</i> <i>op</i> <i>expression</i>}*
<p>The <code>set</code> statement changes the value of a variable <i>var</i> according
to the operator <i>op</i> and the value of the <i>expression</i>. The operators are:
<dl>
<dt>
<code>=</code></dt>
<dd>The value of <i>expression</i> is assigned to <i>var</i>.</p>
<dt><a name="index245"></a><code>+=</code></dt>
<dd>The value of <i>expression</i> is added to <i>var</i>.<br><br>
<dt><a name="index246"></a><code>*=</code></dt>
<dd>The value of <i>var</i> is multiplied by the value of the expression.<br><br>
<dt><a name="index247"></a><code>&amp;=</code></dt>
<dd>The value of <i>expression</i> is inserted as the last element of
the list referenced by <i>var</i>. If <i>var</i> is the empty list (denoted by <code>#f</code>),
then <i>var</i> is assigned a newly constructed list of one element, the value
of <i>expression</i>.<br><br>
<dt><a name="index248"></a><code>^=</code></dt>
<dd>The value of <i>expression</i>, a list, is appended to the list referenced
by <i>var</i>. If <i>var</i> is the empty list (denoted by <code>#f</code>), then <i>var</i>
is assigned the (list) value of <i>expression</i>.<br><br>
<dt><a name="index249"></a><code>@=</code></dt>
<dd>Pushes the value of <i>expression</i> onto the front of the list
referenced by <i>var</i>. If <i>var</i> is empty (denoted by <code>#f</code>), then <i>var</i> 
is assigned a newly constructed list of one element, the value of <i>expression</i>.<br><br>
<dt><a name="index250"></a><code>&lt;=</code></dt>
<dd>Sets the new value of <i>var</i> to the minimum of the old value 
of <i>var</i> and the value of <i>expression</i>.<br><br>
<dt><a name="index251"></a><code>&gt;=</code></dt>
<dd>Sets the new value of <i>var</i> to the maximum of the old value 
of <i>var</i> and the value of <i>expression</i>.
</dd></dl>
<p></p>
<pre>
<i>; example from Rick Taube's SAL description</i>
loop
  with a, b = 0, c = 1, d = {}, e = {}, f = -1, g = 0
  for i below 5
  set a = i, b += 1, c *= 2, d &amp;= i, e @= i, f &lt;= i, g &gt;= i
  finally display "results", a, b, c, d, e, f, g
end
</pre>

<p></p>
<a name = "76"><h5>with</h5></a><a name="index252"></a><code>with</code> <i>var</i> [= <i>expression</i>] {, <i>var</i> [= <i>expression</i>]}*
<p>The <code>with</code> statement declares and initializes local variables. It 
can appear only after <code>begin</code> or <code>loop</code>. If the <i>expression</i> is
omitted, the initial value is <i>false</i>. The variables are visible only 
inside the <code>begin</code>-<code>end</code> or <code>loop</code> statement where the 
<code>with</code> statement appears. Even in <code>loop</code>'s the variables 
are intialized only when the loop is entered, not on each iteration.</p>
<a name = "77"><h5>exit</h5></a><a name="index253"></a><code>exit</code> [<code>nyquist</code>]
<p>The <code>exit</code> statement is unique to Nyquist SAL. It returns from SAL 
mode to the XLISP interpreter. (Return to SAL mode by typing &ldquo;<code>(sal)</code>&rdquo;).
If <code>nyquist</code> is included in the statement, then the entire Nyquist
process will exit.</p>
<a name = "78"><h3>Interoperability of SAL and XLISP</h3></a><a name="index254"></a><a name="index255"></a>
<p>When SAL evaluatas command or loads files, it translates SAL into XLISP.
You can think of SAL as a program that translates everything you write
into XLISP and entering it for you. Thus, when you define a SAL function,
the function actually exists as an XLISP function (created using
Lisp's <code>defun</code> special form). When you set or evaluate global variables
in SAL, these are exactly the same Lisp global variables. Thus, XLISP
functions can call SAL functions and vice-versa. At run time, 
everything is Lisp.</p>
<a name = "79"><h4>Function Calls</h4></a>
<p>In general, there is a very simple translation from SAL to Lisp syntax
and back. A function call is SAL, for example, 
</p>
<pre>
osc(g4, 2.0)
</pre>

<p>
is translated to Lisp by moving the open parenthesis in front of the
function name and removing the commas:
</p>
<pre>
(osc g4 2.0)
</pre>

<p>
Similarly, if you want to translate a Lisp function call to SAL, just
reverse the translation.</p>
<a name = "80"><h4>Symbols and Functions</h4></a>
<p>SAL translates keywords with trailing colons (such as <code>foo:</code>)
into Lisp keywords with leading colons (such as <code>:foo</code>), but
SAL keywords are not treated as expressions as they are in Lisp.
You cannot write <code>open("myfile.txt", direction: output:)</code>
because SAL expects an expression after direction. A special form
<code>keyword</code> is defined to generate a Lisp keyword as an 
expression. The argument is the keyword <i>without</i> a colon, e.g.
<code>open("myfile.txt", direction: keyword(output))</code>. Alternatively, 
you can write the Lisp-style keyword with the leading colon, e.g.
<code>open("myfile.txt", direction: :output)</code>.</p>

<p>In Nyquist SAL, the hash character (#), can be used as a prefix to a 
Lisp function name. For example, the following command is not legal
because <code>print</code> is a SAL command name, not a legal function name:
</p>
<pre>set v = append(print(a), print(b))</pre>

<p>
(Here the intent is to print
arguments to append). However, you can use the hash character to access
the Lisp <code>print</code> function: 
</p>
<pre>set v = append(#print(a), #print(b))</pre>

<p>
<a name = "81"><h4>Playing Tricks On the SAL Compiler</h4></a></p>

<p>In many cases, the close coupling between SAL and XLISP gives SAL
unexpected expressive power. A good example is <code>seqrep</code>. This
is a special looping construct in Nyquist, implemented as a macro in
XLISP. In Lisp, you would write something like:
</p>
<pre>
(seqrep (i 10) (pluck c4))
</pre>

<p>
One might expect SAL would have to define a special <code>seqrep</code>
statement to express this, but since statements do not return values,
this approach would be problematic. The solution (which is already
fully implemented in Nyquist) is to define a
new macro <code>sal-seqrep</code> that is equivalent to <code>seqrep</code> 
except that it is called as follows:
</p>
<pre>
(sal-seqrep i 10 (pluck c4))
</pre>

<p>
The SAL compiler automatically translates the identifier <code>seqrep</code> to 
<code>sal-seqrep</code>. Now, in SAL, you can just write
</p>
<pre>
seqrep(i, 10, pluck(c4))
</pre>

<p>
which is translated in a pretty much semantics-unaware fashion to
</p>
<pre>
(sal-seqrep i 10 (pluck c4))
</pre>

<p>
and viola!, we have Nyquist control constructs in SAL even though SAL
is completely unaware that <code>seqrep</code> is actually a special form.</p>
<hr>
<a href = "part6.html">Previous Section</a> | <a href = "part8.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a>
</body></html>