-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
1642 lines (1557 loc) · 60.7 KB
/
index.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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>DM Script (DMS)</title>
<script
src='https://www.w3.org/Tools/respec/respec-w3c-common'
class='remove'></script>
<script class='remove'>
var respecConfig = {
wg: "Conversational Interfaces Working Group",
wgURI: "https://www.w3.org/community/conv/",
wgPublicList: "public-conv",
specStatus: "CG-FINAL",
editors: [{
name: "Nishant Shukla",
url: "https://shukla.io",
w3cid: 112944
}],
authors: [
{
name: "Nelson Solano",
url: "https://www.linkedin.com/in/nelson-solano-24a935126",
w3cid: 113168
},
{
name: "Victor Zhang",
url: "https://www.linkedin.com/in/victor-zhang-a8b303108",
w3cid: 113469
},
{
name: "James Long",
url: "https://jlongster.com/"
}
],
edDraftURI: "https://github.com/w3c/dms",
shortName: "dms",
addSectionLinks: true,
isPreview: false,
link: {
"check-punctuation": true
},
localBiblio: {
"dms-compiler": {
"authors": [
"Nishant Shukla",
"James Long"
],
"href": "https://github.com/conversational-interfaces/dms-compiler",
"title": "Dialogue Manager Script Compiler"
}
}
};
</script>
<script src="main.js"></script>
<script src="dm.js"></script>
<script src="dms-compiler.js"></script>
</head>
<body>
<section id='abstract'>
<p>
This is the first version of the Dialogue Manager Script (DMS) documentation,
which will demonstrate the current state and capabilities of DMS
through a <a>tutorial</a>, outline the <a>syntax and semantics</a>,
and discuss how to write <a>effective DMS</a>.
DMS is a high-level language that compiles down to [[DMPL]] compliant JSON.
</p>
</section>
<section id='sotd'>
</section>
<section>
<h2>Getting Started</h2>
<p>
The code snippets presented in this page can be evaluated by hitting the "Run" button,
which compiles DM Script into [[DMPL]] JSON, and then processes that JSON on a JavaScript runtime.
The compiler can be found on GitHub [[dms-compiler]].
</p>
</section>
<section>
<h2><dfn>Tutorial</dfn>: Guessing Game</h2>
<p>
This guessing game chat-bot will help us better understand DMS.
Let's build a simple prompt that asks the user for some <a>input</a>.
We start by printing out messages using <code>act</code>, and
then awaiting the user's input.
</p>
<div>
<pre class="example">
act "Guess the number!"
act "Please input your guess."
input -> guess {
_ {
act "You guessed " + guess
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
<input style="display: none;" type="number" placeholder="type input here" />
<button style="display: none;" onclick="return handleIntent(this);">Send</button>
</blockquote>
</div>
<p>
The <code>input -></code> statement awaits for user input, and feeds it to a <a>variable</a> of our choosing.
In this case, the variable <code>guess</code> is populated with the user's input.
</p>
<div>
<pre class="example">
once {
act "Guess the number!"
secret_number = pick(range(1, 101))
}
act "Please input your guess."
input -> guess {
to_num(guess) > secret_number {
act "Too big!"
}
to_num(guess) < secret_number {
act "Too small!"
}
_ {
act "You win!"
pop true
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
<input style="display: none;" type="number" placeholder="type input here" />
<button style="display: none;" onclick="return handleIntent(this);">Send</button>
</blockquote>
</div>
Once the user guesses the correct number, the system will output "You win!",
and finallly the script will end, as indicated by <code>pop true</code>, which
pops the current script from the runtime stack.
</section>
<section>
<h2><dfn>Syntax and Semantics</dfn></h2>
<p>
The DMS syntax is loosely based off Rust.
Curly-brackets <code>{ }</code> establish a block of code,
statements do not need a trailing semi-colon <code>;</code>,
and labels before the curly-brackets annotate the behavior of the block.
Similar to ECMAScript, DMS is not a strongly typed language.
</p>
<p>
Scripts written in DM Script are also called <dfn>components</dfn>.
The runtime shall evaluate the component in an infinite loop,
similar to game-loops inherent in most game engines.
Components may call other components through <a>run</a> or <a>use</a> statements.
</p>
<section>
<h3><dfn>Comments</dfn></h3>
<p>
Programmers may leave notes, or comments, in their code.
These comments are ignored by the compiler,
but are useful for other programmers reading the code.
</p>
<pre class="example">
// Write your comments here.
</pre>
</section>
<section>
<h3><dfn>Variables</dfn></h3>
<p>
In programming, a variable is nothing more than a placeholder for some value.
The <a>strings</a> <code>“Chai”</code>, and <code>“Moo”</code> are simply values that a
placeholder <code>dog_name</code> could take on,
as can be seen in the following example.
</p>
<div>
<pre class="example">
// Initial value for a dog name.
dog_name = "Chai"
// Later on dog_name can be changed to a different value if needed.
dog_name = "Moo"
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Note that in DMS, there is always an implicit infinite loop wrapping our code.
During execution it will keep creating and assigning a variable
named <code>dog_name</code> the values <code>"Chai”</code> and <code>"Moo”</code>.
</p>
</section>
<section>
<h3><dfn>Control Flow</dfn></h3>
<p>
In order to remove these redundant <a>variable</a> creation and assignments
we can wrap code in a block beginning with the keyword <dfn><code>once</code></dfn>.
The <a>once</a> keyword tells the compiler that the variable creation and
assignments should only be executed one time over the lifetime of the <a>component</a>.
</p>
<div>
<pre class="example">
once {
dog_name = "Chai"
}
dog_name = "Moo"
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Deciding whether or not to run some code depending on if a condition is true
is a basic building block in most programming languages.
The most common construct thats allows programmers to control the flow of execution
of DMS code are <a>if statements</a>.
Recall that in DMS there is always an implicit infinite loop wrapping our code,
meaning that loops are inherently built in,
and any code outside of a <a>once</a> block will be repeatedly executed over the lifetime of our program.
</p>
<section>
<h3><dfn>If Statements</dfn></h3>
<p>
An <code>if</code> statement allows us to branch our code depending on conditions.
These statements start with the keyword <code>if</code>, and
follow up with a condition that evaluates to a Boolean value.
</p>
<div>
<pre class="example">
once {
number = 1
}
if number >= 2 {
act "Number is greater than or equal to 2."
}
else {
act "Number is less than 2."
}
number = number + 1
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
We can link multiple conditions by combining <code>if</code> and <code>else</code>
in an <code>else if</code> block. For example:
</p>
<div>
<pre class="example">
once {
number = 7
}
if number % 3 == 0 {
act "Number is divisible by 3."
}
else if number % 2 == 0 {
act "Number is divisible by 2."
}
else {
act "Number is not divisible by 2 or 3."
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
</section>
<section>
<h3><dfn>Fork Statements</dfn></h3>
<p>
Forks are generalized <a>if statements</a>.
They're the heart and soul of DM Script.
As a refresher, let's consider the trivial example below of branching logic.
</p>
<div>
<pre class="example">
if true {
act "Let's talk about animals."
}
else {
act "Let's talk about colors."
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
A <code>fork</code> statement allows a more general way of representing branching behavior.
First, let's recreate the example above using <code>fork</code>.
Think of it as a fork in the road, where we can only go down one candidate path.
Each candidate has an entry-condition.
In the example below, notice that the underscore <code>_</code>
is a shortcut for the Boolean value <code>true</code>.
</p>
<div>
<pre class="example">
fork {
_ {
act "Let's talk about animals."
}
_ {
act "Let's talk about colors."
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
By default, a <code>fork</code> picks the first path whose entry-condition is met,
from top to bottom.
This method for resolving a fork is also called the "greedy" strategy,
because it picks the first possible candidate instead of considering all candidates.
</p>
<p>
Forks allow powerful customization of the branching behavior.
In the example below, the <code>fork</code> picks a child-block at random.
We can optionaly change the <dfn>fork-strategy</dfn> by providing a dictionary
in a decorator <code>#{}</code> directly before the statement.
In the example below, a strategy of <code>{depth: 0}</code>
is associated with the <code>fork</code>.
</p>
<div>
<pre class="example">
#{depth: 0}
fork {
_ {
act "Let's talk about animals."
}
_ {
act "Let's talk about colors."
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
The content of the dictionary specifying the <a>fork-strategy</a> depends
on what the underlying runtime supports.
In this case, <code>{depth: 0}</code> means use bounded depth-first search (BDFS),
with a depth of <code>0</code> to resolve the fork.
A depth of <code>0</code> in BDFS is effectively picking a candidate at random.
Different run-times may support different search algorithms,
such as Monte Carlo Tree Search.
</p>
<p>
The heuristic function for the search algorithm is specified by declaring a <dfn>model</dfn>.
The <a>model</a> of a <a>fork-strategy</a> is a list of lists, representing preferences.
For example, the pair <code>[{x: 0}, {x:10}]</code>, declares that the value of <code>x</code>
is preferred to be <code>0</code> as opposed to <code>10</code>.
Internally, the runtime infers a utility-function, which is a real-valued function over the
variables in DM Script, that provides a total-ordering of preferences.
</p>
<div>
<pre class="example">
once {
x = 5
}
#{depth: 1, model: [[{x: 0}, {x: 10}]]}
fork {
_ {
x = x + 1
}
_ {
x = x - 1
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Changing the preference <a>model</a> changes the program behavior.
For example, by swapping the pair <code>[{x: 10}, {x: 0}]</code>,
the program now counts up instead of counting down.
</p>
<div>
<pre class="example">
once {
x = 5
}
#{depth: 1, model: [[{x: 10}, {x: 0}]]}
fork {
_ {
x = x + 1
}
_ {
x = x - 1
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
If we want to say that the value of <code>x</code> should be <code>5</code>,
we can write the <a>model</a> as a list of preference-pairs, as follows.
</p>
<div>
<pre class="example">
once {
x = 3
}
#{depth: 1, model: [[{x: 5}, {x: 0}], [{x: 5}, {x: 10}]]}
fork {
_ {
x = x + 1
}
_ {
x = x - 1
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Consider the following scenario, where a autonomous agent has 3 possible actions,
(1) eat food, (2) do nothing, or (3) work for food.
In DM Script, we can use <code>fork</code> to define
the pre-conditions and outcomes of each action.
We specify that <code>{health: 10}</code> is desirable over <code>{health: -10}</code>,
and use BDFS to resolve the fork, with a depth of <code>1</code>.
</p>
<div>
<pre class="example">
once {
health = 2
food = 1
}
#{depth: 1, model: [[{health: 10}, {health: -10}]]}
fork {
food > 0 {
act "eat"
food = food - 1
health = health + 3
}
_ {
act "do nothing"
health = health - 1
}
_ {
act "work"
food = food + 1
health = health - 2
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this, false);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Unfortunately, the program chooses the suboptimal sequence of actions:
it chooses to do nothing after eating up all the available food.
Let's increase its intelligence by changing the fork-strategy to <code>{depth: 3}</code>.
Notice that now, the program will alternate between eating food and working,
which is the optimal strategy.
</p>
<div>
<pre class="example">
once {
health = 2
food = 1
}
#{depth: 3, model: [[{health: 10}, {health: -10}]]}
fork {
food > 0 {
act "eat"
food = food - 1
health = health + 3
}
_ {
act "do nothing"
health = health - 1
}
_ {
act "work"
food = food + 1
health = health - 2
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this, false);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
We'll cover more interesting use-cases of <code>fork</code> in the <a>effective DMS</a> section.
</p>
</section>
<section>
<h3><dfn>Primitive Types</dfn></h3>
<p>
The example in the <a>variables</a> section only allowed the variable <code>dog_name</code>
to take on <a>string</a> values.
However, DMS is a dynamically typed language meaning that variables can
take on any basic data type such as <dfn><code>string</code></dfn>, <dfn><code>float</code></dfn>, and
<dfn><a>Boolean</a></dfn>.
Consider the following example where we have a variable named
<code>current_thought</code> which denotes what a programmer might be
thinking about throughout the day.
</p>
<div>
<pre class="example">
once {
// Early in the morning their first thought could potentially be
current_thought = "coffee"
// Next they could be thinking, do I want office coffee?
current_thought = false
// Note that we don't use double quotes when assigning a boolean value to a variable.
// And so they decide to buy coffee elsewhere, which has an associated cost.
current_thought = 5.20
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
Every value in DMS has a data type which tells DMS what kind of data is being specified
so it knows how to work with the data.
Recall that DMS is dynamically typed meaning that it doesn’t need to know the
types of all variables at compile time.
</section>
<section>
<h3><dfn>Structures</dfn></h3>
<p>
The <a>primitive types</a> defined in the previous section are all <dfn>atomic</dfn> literals.
DMS also allows programmers to build more complex structures such as lists, or dictionaries.
</p>
<section>
<h3><dfn>List</dfn></h3>
<p>
A <code>list</code> is an ordered arrangement of other <a>structures</a>.
All elements are enclosed within square-brackets <code>[ ]</code>,
and separated by commas as follows.
</p>
<div>
<pre class="example">
once {
// list of integers
student_grades = [87, 90, 88, 92, 93]
// list of strings
student_names = ["Nawar", "Tom", "Chris"]
// list of arbitrary types
a_bunch_of_stuff = [false, 1, "two", [3, 4, 5]]
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>Dictionary</dfn></h3>
<p>
A <code>dictionary</code> is a <a>structure</a> that maps data in key-value pairs.
The value of a corresponding key can be any <a>structure</a>.
</p>
<div>
<pre class="example">
once {
student_grades = {
Nishant: 0,
Carol: 93,
Daniel: 90
}
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Take note of the syntax that was used to create the <code>dictionary</code>
in the above code snippet.
The keys of the <code>dictionary</code> are symbols which are a finite sequence
of characters without the double quotation-marks,
and the values in this example are simply integer values.
Note that when accessing the values of a <code>dictionary</code>,
the keys must be enclosed within double quotation marks.
</p>
<div>
<pre class="example">
once {
student_grades = {
Nishant: 0,
Carol: 93,
Daniel: 90
}
act student_grades["Nishant"]
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
</section>
<section>
<h3><dfn>Functions</dfn></h3>
<p>
Custom function definitions in DMS start with the keyword <code>def</code> and have a set of
parentheses after the function name.
The curly-brackets <code>{ }</code> tell the compiler where the function body begins and ends.
Lastly, a <dfn><code>pop</code></dfn> statement returns the value to be used by the caller.
Consider the following function which simply returns the <a>string</a>, <code>"Greetings!"</code>.
</p>
<pre class="example">
def greet() {
pop "Greetings!"
}
</pre>
<p>
Our declaration of the <code>greet</code> function begins with the keyword <code>def</code>,
is followed by the function name <code>greet</code>, and ends with a set of empty parentheses <code>()</code>.
The function body contains all the logic that is expected to be executed when calling the function.
In the above example, we simply return the <a>string</a> <code>"Greetings!"</code> using the keyword <a>pop</a>.
In order to call our function and print the corresponding greeting, we use the keyword <dfn><code>act</code></dfn>.
</p>
<div>
<pre class="example">
once {
def greet() {
pop "Greetings!"
}
act greet()
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Functions in DMS can also have parameters,
which are special variables that are part of the function declaration.
The following re-declaration of the greet function allows programmers to pass in a variable of their choice,
such as the name of a user in the following example.
</p>
<div>
<pre class="example">
once {
programmer = "Naitian"
def greet(user) {
pop "Greetings " + user
}
}
act greet(programmer)
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
The variables introduced in a function are local variables that are
only accessible within the current scope, and they shadow the variables
defined in the outer scope.
In the example below, we define a variable called <code>greetings</code>
within a function, and later try to access that variable out of scope.
Undefined variables by default evaluate to <dfn><code>null</code></dfn>.
</p>
<div>
<pre class="example">
once {
programmer = "Naitian"
def greet(user) {
greetings = pick(["Greetings", "Howdy", "Sup"])
pop greetings + " " + user
}
act greet(programmer)
act programmer
act greetings
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
You can also implement <dfn>closure</dfn> using <a>functions</a>. For example, in the following code-snippet we show how
to define a function that raises a number to an exponent.
</p>
<div>
<pre class="example">
once {
def powerOf(power) {
def f(num) {
pop pow(num, power)
}
pop f
}
square = powerOf(2)
cube = powerOf(3)
act square(3)
act cube(3)
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
In DMS, functions are pure, so <a>act</a> or <a>input</a>
statements are not recommended in the body of user defined functions.
</p>
<section>
<h3><dfn>Built-in Functions</dfn></h3>
A handful of functions are available out of the box in DMS, forming a standard library.
The sections below cover the essential functions.
<section>
<h3><dfn>Comparison operators</dfn></h3>
Check for equality with <code>==</code> or <code>!=</code>.
Compare <code>numbers</code> with <code>></code>, <code>>=</code>, <code><</code>, and <code><=</code>.
<div>
<pre class="example">
once {
x = 1
y = 2
act x == y
act x != y
act x < y
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>Boolean operators</dfn> </h3>
Typical <code>Boolean</code> functions such as <code>&&</code>, <code>||</code>, and <code>!</code> are supported.
<div>
<pre class="example">
once {
x = true
act x || false
act (x && !x) == false
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>Arithmetic operations</dfn></h3>
Frequently used mathematical functions over <code>numbers</code> are available, such as
<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>, and <code>floor</code>.
<div>
<pre class="example">
once {
x = (1 + 2 * 3) / 4
y = (x + 0.25) % 2
z = floor(x)
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>Type casting operators</dfn></h3>
The <code>to_num</code> and <code>to_str</code> operators allow changing types between <code>numbers</code>
and <code>strings</code>.
<div>
<pre class="example">
once {
number = 13.5
act "The number is " + to_str(number)
guess = "10"
act "You're off by..."
act number - to_num(guess)
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>++</dfn></h3>
<p>
The <code>++</code> operator concatenates two <a>lists</a>.
Note, on the other hand, the <code>+</code> operator combines <code>strings</code>.
</p>
<div>
<pre class="example">
once {
act [1, 2, 3] ++ [4, 5, 6]
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>len</dfn></h3>
<p>
The <code>len</code> function returns the length of the given argument.
Possible argument types include: <code>string</code>, <a>list</a>, or <a>dictionary</a>.
</p>
<div>
<pre class="example">
once {
school_data = {
student_names: ["Jeremy", "Earle", "Chad"],
school_name: "Institute of Good Learning"
}
act len(school_data)
act len(school_data["student_names"])
act len(school_data["student_names"][0])
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>pick</dfn></h3>
<p>
The <code>pick</code> function allows programmers to pseudo-randomly select an item from a <a>list</a>.
</p>
<div>
<pre class="example">
once {
athlete_rankings = [
{ lonzo: 2 },
{ zion: 1 }
]
}
act pick(athlete_rankings)
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>get</dfn></h3>
<p>
The <code>get</code> function allows users to select a specific element of a <a>list</a>.
In the above example if we wanted to retrieve the last element we could do so as follows.
</p>
<div>
<pre class="example">
act get(len(athlete_rankings) - 1, athlete_rankings)
</pre>
</div>
<p>
Or, use the <code>[ ]</code> notation, which is just a syntactic-sugar of <code>get</code>, as shown below.
</p>
<div>
<pre class="example">
act athlete_rankings[len(athlete_rankings) - 1]
</pre>
</div>
</section>
<section>
<h3><dfn>range</dfn></h3>
<p>
The <code>range</code> function generates a <a>list</a> from the starting value (inclusive) to the ending value (exclusive).
</p>
<div>
<pre class="example">
once {
act range(1, 11)
act range(1, 5) ++ range(5, 11)
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>An optional third argument specifies the step-size.</p>
<div>
<pre class="example">
once {
act range(1, 11, 2)
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
</section>
<section>
<h3><dfn>map</dfn></h3>
<p>
The <code>map</code> function iterates through a <a>list</a> passed in, and produces a new <a>list</a> with modified elements.
</p>
<div>
<pre class="example">
once {
act map(to_str, [1, 2, 3])
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>
Multiple <a>lists</a> may be passed in, as long as the operator passed in supports that number of arguments.
</p>
<div>
<pre class="example">
once {
act map("+", [1, 2, 3], [4, 5, 6])
}
</pre>
<blockquote>
<button onclick="return runClicked(this);">Run</button>
<pre></pre>
</blockquote>
</div>
<p>This opens up a lot of useful <a>list</a> manipulation techniques, such as implementing <code>zip</code> below.</p>