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

SICPy §1

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

What names are predeclared in SICPy §1?

MATH: Mathematical constants and functions

MISC: Miscellaneous constants and functions

What can you do in SICPy §1?

All features in SICPy §0 are inherited.

Assignments

Assignment statements in SICPy are done in:

a = x + 2

Here the name a gets assigned within the surrounding block, and refers to the result of evaluating x + 2 in the rest of the block. You can read more about the scope of names in section 1.1.8 Functions as Black-Box Abstractions.

Conditional Statements

Conditional evaluation is also possible within statements, for example the body of a function declaration. For that, you can use conditional statements, for example:

if x == 1 :
    return x + 2
elif x == 2 :
    return x * 2
else :
    return x / 3

Read about conditional statements in section 1.3.2 Function Definition Expressions.

Function declarations and function definitions

A function declaration is a statement that declares a name and binds it to a function. For example

def square(x) :
    return x * x

declares the name square and binds it to a squaring function, so that it can be applied as in square(5). You can read about function declaration statements in textbook section 1.1.4 Functions.

Sometimes, it's not necessary to give a name to a function: You may want to create a function only to pass it to some other function as argument. For that, SICPy supports function definition expressions. For example

(lambda x : x * x)(3) // returns 9

creates a square function just like the function declaration above, but does not give it a name. Its only purpose it to be applied to the number 3. See also textbook section 1.3.2 Function Definition Expressions.

Blocks

Blocks make up the bodies of functions and the consequent and alternative statements of conditional statements. Blocks are differentiated through the level of indentations. The number of indentation within a block must always be consistent. The following example shows nested if-statements.

a = 1
if True:
   a = 2
   print(a)
   if a == 2:
        a = 3
        print(a)

Note: You CANNOT use blocks elsewhere in your program, if you want to declare constants local to a specific scope.

Specifications

Refer to this document for the specifications.