|
| 1 | +Source §1 is a small programming language, designed for the first chapter |
| 2 | +of the textbook |
| 3 | +<a href="https://sicp.comp.nus.edu.sg">Structure and Interpretation |
| 4 | +of Computer Programs, JavaScript Adaptation</a> (SICP JS). |
| 5 | + |
| 6 | +## What names are predeclared in Source §1? |
| 7 | + |
| 8 | +On the right, you see all predeclared names of Source §1, in alphabetical |
| 9 | +order. Click on a name to see how it is defined and used. |
| 10 | +These names come in two groups: |
| 11 | + <ul> |
| 12 | + <li> |
| 13 | + <a href="../MISC/index.html">MISC</a>: Miscellaneous constants and functions |
| 14 | + </li> |
| 15 | + <li> |
| 16 | + <a href="../MATH/index.html">MATH</a>: Mathematical constants and functions |
| 17 | + </li> |
| 18 | + </ul> |
| 19 | +In addition, |
| 20 | +<a href="https://sicp.comp.nus.edu.sg/chapters/33">section 2.2.4 SICP JS</a> makes |
| 21 | +use of <a href="../RUNES/index.html">RUNES</a>, a library for runes graphics. |
| 22 | + |
| 23 | +## What can you do in Source §1? |
| 24 | + |
| 25 | +You can use all features that are introduced in |
| 26 | +<a href="https://sicp.comp.nus.edu.sg/chapters/1">chapter 1</a> of the |
| 27 | +textbook. Below is the list of features, each with a link to the |
| 28 | +textbook section that introduces it and a small example. |
| 29 | + |
| 30 | +### Literal values |
| 31 | + |
| 32 | +Literal values are simple expressions that directly evaluate to values. These |
| 33 | +include numbers in the usual decimal notation, the two boolean values |
| 34 | +`true` and `false`, and the predeclared names |
| 35 | +`NaN`, `Infinity` and `undefined`. |
| 36 | +More on literal values in <a href="https://sicp.comp.nus.edu.sg/chapters/2">section |
| 37 | +1.1 The Elements of Programming</a> of the textbook. |
| 38 | + |
| 39 | +### Constant declarations |
| 40 | + |
| 41 | +Constant declarations are done in Source with <PRE><CODE>const my_name = x + 2;</CODE></PRE> |
| 42 | +Here the name `my_name` gets declared within the surrounding block, |
| 43 | +and refers to the result of evaluating `x + 2` in the rest of the block. |
| 44 | +You can read more about the <EM>scope of names</EM> in |
| 45 | +<a href="https://sicp.comp.nus.edu.sg/chapters/10">section 1.1.8 |
| 46 | +Functions as Black-Box Abstractions</a>. |
| 47 | + |
| 48 | +### Conditional statements and conditional expressions |
| 49 | + |
| 50 | +Within expressions, you can let a <EM>predicate</EM> determine whether |
| 51 | +a <EM>consequent expression</EM> |
| 52 | +gets evaluated or an <EM>alternative expression</EM>. This is done by writing, |
| 53 | +for example |
| 54 | +<PRE><CODE>return p(x) ? 7 : f(y);</CODE></PRE> |
| 55 | +Read more on conditional expressions in |
| 56 | +<a href="https://sicp.comp.nus.edu.sg/chapters/8">section 1.1.6 |
| 57 | +Conditional Expressions and Predicates</a>. |
| 58 | +<EM>Conditional evaluation</EM> is also possible within statements, for |
| 59 | +example the body of a function declaration. For that, you can use <EM>conditional |
| 60 | +statements</EM>, for example:<PRE><CODE>if (p(x)) { |
| 61 | + return 7; |
| 62 | +} else { |
| 63 | + return f(y); |
| 64 | +}</CODE></PRE> |
| 65 | +Read about <EM>conditional statements</EM> in |
| 66 | +<a href="https://sicp.comp.nus.edu.sg/chapters/20">section 1.3.2 |
| 67 | +Function Definition Expressions</a>. |
| 68 | + |
| 69 | +### Function declarations and function definitions |
| 70 | + |
| 71 | +A function declaration is a statement that declares a name and binds it |
| 72 | +to a function. For example |
| 73 | +<PRE><CODE>function square(x) { |
| 74 | + return x * x; |
| 75 | +}</CODE> |
| 76 | +</PRE> |
| 77 | +declares the name `square` and binds it to a squaring function, so that it can be applied |
| 78 | +as in `square(5);`. You can read about function declaration statements in textbook |
| 79 | +<a href="https://sicp.comp.nus.edu.sg/chapters/6">section 1.1.4 Functions</a>. |
| 80 | + |
| 81 | +Sometimes, it's not necessary to give a name to a function: You may |
| 82 | +want to create a function only to pass it to some other function as argument. |
| 83 | +For that, Source |
| 84 | +supports function definition expressions. For example |
| 85 | +<PRE><CODE>(x => x * x)(3); // returns 9</CODE> |
| 86 | +</PRE> |
| 87 | +creates a square function just like the function declaration above, |
| 88 | +but does not give it a name. |
| 89 | +Its only purpose it to be applied to the number 3. See also |
| 90 | +textbook |
| 91 | +<a href="https://sicp.comp.nus.edu.sg/chapters/20">section 1.3.2 Function Definition Expressions</a>. |
| 92 | + |
| 93 | +### Blocks |
| 94 | + |
| 95 | +Blocks make up the bodies of functions and the consequent and alternative statements of |
| 96 | +conditional statements. You can use blocks also elsewhere in your program, if you |
| 97 | +want to declare constants local to a specific scope. For example in this program |
| 98 | +<PRE><CODE>const a = 1; |
| 99 | +{ |
| 100 | + const a = 2; |
| 101 | + display(a); |
| 102 | +} |
| 103 | +display(a);</CODE> |
| 104 | +</PRE> |
| 105 | +the first application of `display` shows the value 2, because the |
| 106 | +declaration <B>const</B> `a = 2;` re-declares the constant `a`. |
| 107 | +However, the second application |
| 108 | +of `display` shows the value 1, because |
| 109 | +the declaration <B>const</B> `a = 2;` is limited in scope by its surrounding block. |
| 110 | +You can read more about <EM>blocks</EM> in |
| 111 | +<a href="https://sicp.comp.nus.edu.sg/chapters/10">section 1.1.8 |
| 112 | +Functions as Black-Box Abstractions</a>. |
| 113 | + |
| 114 | +### Boolean operators |
| 115 | + |
| 116 | +Boolean operators in Source have a special meaning. Usually, an operator combination |
| 117 | +evaluates all its arguments and then applies the operation to which the operator refers. |
| 118 | +For example, `(2 * 3) + (4 * 5)` evaluates `2 * 3` and `4 * 5` first, before the addition |
| 119 | +is carried out. However, the operator `&&` works differently. An expression |
| 120 | +`e1 && e2` should be seen as an abbreviation for `e1 ? e2 : false`. The expression |
| 121 | +`e2` only gets evaluated if `e1` evaluates to `true`. The behaviour of `||` is similar: |
| 122 | +`e1 || e2` should be seen as an abbreviation for `e1 ? true : e2`. More on these |
| 123 | +two boolean operators in textbook |
| 124 | +<a href="https://sicp.comp.nus.edu.sg/chapters/8">section 1.1.6 Conditional |
| 125 | +Expressions and Predicates</a>. |
| 126 | + |
| 127 | +### Sequences |
| 128 | + |
| 129 | +A program or the body of a block does not need to consist of a single statement. |
| 130 | +You can write multiple statements in a row. In the REPL ("Read-Eval-Print-Loop") |
| 131 | +of a Source implementation, you can write |
| 132 | +<PRE><CODE>cube(7); |
| 133 | +square(5);</CODE></PRE> |
| 134 | +The statements in such a sequence are evaluated in the given order. The |
| 135 | +result of evaluating the sequence is the result of evaluating the last |
| 136 | +statement in the sequence, in this case `square(5);`. |
| 137 | +Read more about sequences in |
| 138 | +<a href="https://sicp.comp.nus.edu.sg/chapters/4">section 1.1.2 |
| 139 | +Naming and the Environment</a> of the textbook. |
| 140 | + |
| 141 | +## You want the definitive specs? |
| 142 | + |
| 143 | +For our development team, we are maintaining a definitive description |
| 144 | +of the language, called the |
| 145 | +<a href="../source_1.pdf">Specification of Source §1</a>. Feel free to |
| 146 | +take a peek. |
| 147 | + |
0 commit comments