Skip to content

Commit 2016033

Browse files
committed
updated README_1 and MATH
1 parent 3442927 commit 2016033

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

docs/md/README_1.md

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ of the textbook
33
<a href="https://sourceacademy.org/sicpjs">Structure and Interpretation
44
of Computer Programs, JavaScript Adaptation</a> (SICP JS).
55

6-
## What names are predeclared in Source §1?
6+
## What names are predeclared in Python §1?
77

88
On the right, you see all predeclared names of Python §1, in alphabetical
99
order. Click on a name to see how it is defined and used.
@@ -48,17 +48,18 @@ Within expressions, you can let a <EM>predicate</EM> determine whether
4848
a <EM>consequent expression</EM>
4949
gets evaluated or an <EM>alternative expression</EM>. This is done by writing,
5050
for example
51-
<PRE><CODE>return p(x) ? 7 : f(y);</CODE></PRE>
51+
<PRE><CODE>return 7 if p() else f(y)</CODE></PRE>
5252
Read more on conditional expressions in
5353
<a href="https://sourceacademy.org/sicpjs/1.1.6">section 1.1.6
5454
Conditional Expressions and Predicates</a>.
5555
<EM>Conditional evaluation</EM> is also possible within statements, for
5656
example the body of a function declaration. For that, you can use <EM>conditional
57-
statements</EM>, for example:<PRE><CODE>if (p(x)) {
58-
return 7;
59-
} else {
60-
return f(y);
61-
}</CODE></PRE>
57+
statements</EM>, for example:
58+
<PRE><CODE>if p(x):
59+
return 7
60+
else:
61+
return f(y)
62+
</CODE></PRE>
6263
Read about <EM>conditional statements</EM> in
6364
<a href="https://sourceacademy.org/sicpjs/1.3.2">section 1.3.2
6465
Function Definition Expressions</a>.
@@ -67,56 +68,57 @@ Function Definition Expressions</a>.
6768

6869
A function declaration is a statement that declares a name and binds it
6970
to a function. For example
70-
<PRE><CODE>function square(x) {
71-
return x * x;
72-
}</CODE>
71+
<PRE><CODE>def square(x):
72+
return x * x
73+
</CODE>
7374
</PRE>
7475
declares the name `square` and binds it to a squaring function, so that it can be applied
75-
as in `square(5);`. You can read about function declaration statements in textbook
76+
as in `square(5)`. You can read about function declaration statements in textbook
7677
<a href="https://sourceacademy.org/sicpjs/1.1.4">section 1.1.4 Functions</a>.
7778

7879
Sometimes, it's not necessary to give a name to a function: You may
7980
want to create a function only to pass it to some other function as argument.
80-
For that, Source
81+
For that, Python §
8182
supports function definition expressions. For example
82-
<PRE><CODE>(x => x * x)(3); // returns 9</CODE>
83+
<PRE><CODE>(lambda x: x * x)(3) // returns 9</CODE>
8384
</PRE>
8485
creates a square function just like the function declaration above,
8586
but does not give it a name.
8687
Its only purpose it to be applied to the number 3. See also
8788
textbook
8889
<a href="https://sourceacademy.org/sicpjs/1.3.2">section 1.3.2 Function Definition Expressions</a>.
8990

90-
### Blocks
91-
92-
Blocks make up the bodies of functions and the consequent and alternative statements of
93-
conditional statements. You can use blocks also elsewhere in your program, if you
94-
want to declare constants local to a specific scope. For example in this program
95-
<PRE><CODE>const a = 1;
96-
{
97-
const a = 2;
98-
display(a);
99-
}
100-
display(a);</CODE>
91+
### Scope of declaration
92+
93+
A variable declared outside a function, the variable has a global scope. If you want to
94+
declare a variable local to a specific scope, you can declare it in a function.
95+
For example in this program
96+
<PRE><CODE>a = 1
97+
98+
def local_scope():
99+
a = 2
100+
print(a)
101+
local_scope()
102+
print(a)</CODE>
101103
</PRE>
102-
the first application of `display` shows the value 2, because the
103-
declaration <B>const</B> `a = 2;` re-declares the constant `a`.
104+
the first application of `print` shows the value 2, because the
105+
<B>assignment</B> `a = 2` re-declares the variable `a`.
104106
However, the second application
105-
of `display` shows the value 1, because
106-
the declaration <B>const</B> `a = 2;` is limited in scope by its surrounding block.
107-
You can read more about <EM>blocks</EM> in
107+
of `print` shows the value 1, because
108+
the <B>assignment</B> `a = 2` is limited in local scope by defining it in the function.
109+
You can read more about <EM>scope</EM> in
108110
<a href="https://sourceacademy.org/sicpjs/1.1.8">section 1.1.8
109111
Functions as Black-Box Abstractions</a>.
110112

111113
### Boolean operators
112114

113-
Boolean operators in Source have a special meaning. Usually, an operator combination
115+
Boolean operators in Python § have a special meaning. Usually, an operator combination
114116
evaluates all its arguments and then applies the operation to which the operator refers.
115-
For example, `(2 * 3) + (4 * 5)` evaluates `2 * 3` and `4 * 5` first, before the addition
116-
is carried out. However, the operator `&&` works differently. An expression
117-
`e1 && e2` should be seen as an abbreviation for `e1 ? e2 : false`. The expression
118-
`e2` only gets evaluated if `e1` evaluates to `true`. The behaviour of `||` is similar:
119-
`e1 || e2` should be seen as an abbreviation for `e1 ? true : e2`. More on these
117+
For example, `(2 * 3) + (4 * 5)` evaluates to `2 * 3` and `4 * 5` first, before the addition
118+
is carried out. However, the operator <B>and</B> works differently. An expression
119+
`e1 and e2` should be seen as an abbreviation for `e2 if e1 else False`. The expression
120+
`e2` only gets evaluated if `e1` evaluates to `True`. The behaviour of <B>or</B> operator is similar:
121+
`e1 or e2` should be seen as an abbreviation for `True if e1 else e2`. More on these
120122
two boolean operators in textbook
121123
<a href="https://sourceacademy.org/sicpjs/1.1.6">section 1.1.6 Conditional
122124
Expressions and Predicates</a>.
@@ -125,12 +127,12 @@ Expressions and Predicates</a>.
125127

126128
A program or the body of a block does not need to consist of a single statement.
127129
You can write multiple statements in a row. In the REPL ("Read-Eval-Print-Loop")
128-
of a Source implementation, you can write
129-
<PRE><CODE>cube(7);
130-
square(5);</CODE></PRE>
130+
of a Python § implementation, you can write
131+
<PRE><CODE>cube(7)
132+
square(5)</CODE></PRE>
131133
The statements in such a sequence are evaluated in the given order. The
132134
result of evaluating the sequence is the result of evaluating the last
133-
statement in the sequence, in this case `square(5);`.
135+
statement in the sequence, in this case `square(5)`.
134136
Read more about sequences in
135137
<a href="https://sourceacademy.org/sicpjs/1.1.2">section 1.1.2
136138
Naming and the Environment</a> of the textbook.
@@ -139,6 +141,6 @@ Naming and the Environment</a> of the textbook.
139141

140142
For our development team, we are maintaining a definitive description
141143
of the language, called the
142-
<a href="../source_1.pdf">Specification of Source §1</a>. Feel free to
144+
<a href="../python_1.pdf">Specification of Python §1</a>. Feel free to
143145
take a peek.
144146

docs/md/README_MATH.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
All names in the JavaScript `Math` library are predeclared in Source. The
1+
All names in the Python `Math` module are predeclared in Python §. The
22
complete specifications are given in
3-
<a href="https://www.ecma-international.org/ecma-262/9.0/index.html#sec-math-object">ECMAScript
4-
Specification, Section 20.2</a>, and a summary is available here, by clicking
3+
<a href="https://docs.python.org/3/library/math.html">Python
4+
Standard Library Math Documentation</a>, and a summary is available here, by clicking
55
on the names on the right.
6-
<B>As learner of Source, you are not expected to learn all of these, right away. But
6+
<B>As learner of Python §, you are not expected to learn all of these, right away. But
77
you might want to remember where you can look for them: Here!</B>
88
Click on a name to see how it is defined and used.
99
<P/>
1010
Note that we expect
1111
all arguments of `math_...`
12-
functions to be numbers, as defined by the function `is_number`. An implementation
13-
of Source does not need to check whether all arguments of `math_...`
14-
functions are indeed numbers.
12+
functions to be either an integer or float, as defined by the function `is_int` and `is_float`. An implementation
13+
of Python § does not need to check whether all arguments of `math_...`
14+
functions are indeed integer or float.
1515
<P/>
16-
Two of the specifications make use of the function ToUint32, which is defined as follows:
17-
ToUint32 converts argument to one of 2<SUP>32</SUP> integer values in the
18-
range 0 through 2<SUP>32</SUP>-1, inclusive. This operation functions as follows:
19-
20-
If the argument x is NaN, +0, -0, +∞, or -∞, ToUint32(x) return +0.
21-
Otherwise, let `int` be the mathematical value that is the same sign as number and
22-
whose magnitude is `math_floor(math_abs(x))`. ToUint32(x) returns `int` modulo 2<SUP>32</SUP>.

0 commit comments

Comments
 (0)