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

SICPy §4

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

What names are predeclared in SICPy §4?

MATH: Mathematical constants and functions

MISC: Miscellaneous constants and functions

LISTS: Support for lists

PAIRMUTATORS: Mutating pairs

ARRAYS: N/A

STREAMS: Work-in-progress...

MCE: Work-in-progress...

What can you do in SICPy §4?

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

On top of that, we introduce additional syntaxes for SICPy §4 that are not in the textbook.

Tuples

A tuple is a immutable data structure. One can perform tuple-like such assignments:

x,y,z = 1,2,3 
w = 1,2,3 # (1, 2, 3)

Accessing tuples are similar to access Python lists via the indexes.

y = x[0] + x[1] # expect 3

Tuples is not covered in the textbook.

Except Handling

Exception handling is a vital component for programs so that programs would not terminate in the event of errors, such as when one performs division by zero. A try-statement can have more than one except clause. More info can be found here.

try:
    1/0
except e:
    print(e)

Exception handling is not covered in the textbook.

Python Dictionaries

Dictionaries are also known as hash maps in other programming languages. A dictionary can be created using literal dictionary expressions, example:

x = {"apple" : 1, "orange": 2}

A dictionary can be access using dictionary access expressions, using key:

y = x["apple"] # expect 1

Like pairs/lists, dictionaries can be mutated in SICPy §4. This is done using dictionary assignment:

x["orange"] = 9 
y = x["apple"] + x["orange"] # expect 10

Accessing a key that has not been assigned yet will result in an error.

Dictionaries are not covered in the textbook.

Specifications

Refer to this document for the specifications.