SICPy_3 - nus-cs4215/x-slang-t1-xz-jj GitHub Wiki

SICPy §3

SICPy §3 is a small programming language, adapted from Source §3, designed for the third chapter of the textbook Structure and Interpretation of Computer Programs, JavaScript Adaptation (SICP JS).

What names are predeclared in SICPy §3?

MATH: Mathematical constants and functions

MISC: Miscellaneous constants and functions

LISTS: Support for lists

PAIRMUTATORS: Mutating pairs

ARRAYS: N/A

STREAMS: Work-in-progress...

What can you do in SICPy §3?

You can use all features of SICPy §2 and all features that are introduced in chapter 3 of the textbook. Below are the features that SICPy §3 adds to SICPy §2.

While loops

A while loop repeatedly evaluates a predicate and if the predicate returns true, evaluates a given block. The evaluation terminates when the predicate returns false. Example:

x = 0
while x < 10 :
    print(x)
    x = x + 1

will display the numbers from 0 to 9. While loops are not covered in the textbook.

For loops

For loops iterate through iterables (e.g., lists). An example of iterating through a range of numbers is as follow:

for x in range(10):
    print(x)

For loops are not covered in the textbook.

Python Lists - access, assignment, comprehension

In SICPy, arrays are realized using the Python list. To avoid confusion, the list mentioned in this section refers to Python's list.

Lists are created using literal list expressions, as follows:

my_array = [10, 20, 30]

my_array now refers to a list with three elements. The elements in such a literal array expressions have implicit keys. The first element has key 0, the second has key 1, the third has key 2 and so on.

A list can be accessed using list expressions, with a given key:

my_array[0] + my_array[1] + my_array[2] # expect 60

Like pairs, lists can be changed in SICPy §3. This is done using list assignment:

my_array[1] = 200

You can use any list index in list assignment; the list will automatically adjust its size. Accessing an list at an list index that has not been assigned yet (using a literal list expression or an list assignment) will return an error.

Additionally, list comprehensions allow one to return a new list by iterating the list and performing certain operations with certain conditions. Example:

x = [i + 1 for i in my_array]
x # expect [11, 21, 31]

Lists (arrays in SICP terms) are not covered in the textbook.

Specifications

Refer to this document for the specifications.