a first programming language (notes)

Pigeon: a “fake” language. No one actually uses it, but we’re going to learn it anyway.

Source code: the text that the programmer writes in a text editor. To get a runnable program from source code, we need to run it through some program that does a translation.

Comment: text in code that is ignored by the language so that we can write notes in code

Value: a piece of data. The basic data types in Pigeon are numbers, strings, and booleans.

Numbers: in Pigeon, numbers simply must be rational.

String: a value which is a piece of text, i.e. a sequence of characters. Written as a literal in double quote marks, e.g. “She sells sea shells by the sea shore.” denotes a string beginning with a capital S and ending with a period.

Boolean: a data type that has just two values: true and false

Null: the value representing nothing

Literal: a value written in source code

Escape sequence: a special sequence of characters in a string that denotes a special character. The most important are:

\”            double quote

\n           newline

\t            tab

\\            backslash

Operation: an action which takes in zero or more input values and returns out an output value (which may be the value null)

Operator: symbol or word that specifies the operation to perform

Operand: input to an operation

Prefix notation: each operation is written inside parentheses in the form:

(operator operands)

…where the zero or more operands are separated from each other by spaces, e.g.:

(foo a b c)

…denotes the operation foo with operands a, b, and c, while:

(foo a b c)

…denotes the operation foo with no operands.

Because an operation returns a value, operations can be used as operands, e.g.:

(foo a (bar) c)

…here the operation bar with no operands is itself the second operand to the operation foo.

§

Variable: a named location in memory for holding a value.

Identifier: a name for a variable or function

Identifiers can only contain letters of the alphabet or numerals, and an identifier must begin with a letter.

Identifiers are case sensitive, meaning the case of each letter matters: Foo, fOo, foo, and fOO are all different identifiers.

Reserved word: an identifier given special meaning by the language and which you can’t use as a variable or function name. In Pigeon, the operators are given reserved word names.

Assignment: storing a value in a variable. In Pigeon, this is done with an as statement:

as variable value

For example:

as cat 3

…assigns the value 3 to the variable named cat.

Expression: anything which evaluates into a value—namely a variable, a literal, or an operation.

Statement: a Pigeon program is mainly a list of statements. Statements come in different kinds:

  • expression statements
  • as statements
  • if-elif-else statements
  • while statements

(It’s debateable whether we should consider a function definition to be a kind of statement: we usually think of statements as doing something at runtime whereas function definitions simply establish what happens when a particular function is invoked without otherwise doing anything.)

CPU: Central Processor Unit, the chip which executes the instructions of a program

Memory: stores the code and data of all running programs

I/O (input/output) devices: everything else in the system

print: operator that displays a value on the screen

prompt: operator that gets text typed by the user

§

eq: equality test operator

(eq 3 3.0 3)   # true
(eq 3 4)    # false

not: operator that returns opposite truth value

(not true)   # false
(not false)   # true

lt / gt / lte / gte: the less than, greater than, less-than-or-equal-to, and greater-than-or-equal-to-operators

if / elif / else: conditional execution statement in this form:

if condition
    body
elif condition
     body
else
    body

…where condition is some expression evaluating into true or false, and body is some number of statements. An if statement can have any number of elif clauses but only zero or one else clause, which always comes last. Only one body per if is executed: the conditions are tested top to bottom, and the body of the first condition to test true is the body that’s executed; the else acts as a default case that executes only when all conditions are false. If all conditions test false and there’s no else clause, no body is executed.

§

while: like if, but repeats until condition tests false

and: operator that returns true when all of its operands are true; otherwise returns false

or: operator that returns true when any of its operands are true; otherwise returns false

mod: modulus operator returns the remainder of division

(17 mod 3)    # 2

function: an operation defined by the programmer

function name parameters
      body

Functions can be treated as values.

argument: input value to a function call

parameter: variable of function which receives one of the arguments

return: statement that returns a value from the function

§

scope: place and time in which a thing (namely, a variable) exists

namespace: a “space” in which a set of names exist and in which each individual name only refers to one thing, not multiple things

namespace collision: an ambiguity of a name having more than one meaning in a single namespace

global: a variable that exists everywhere in a program and so can be seen in all functions

Functions are values—they can be assigned to variables.

recursive function: a function that calls itself (either directly or indirectly)

§

collection: a value made up of other values

list: a numerically-indexed collection

(list)       # returns an empty list
(list 3 5 “yo”)      # returns a list with three items

key: a value that acts as an index to another value

key-value pair: a key and its associated value

dictionary: an associative collection, i.e. a collection of key-value pairs

(dict)     # returns an empty dictionary
(dict 77 “yo” “avast” true)   # returns a dictionary with 2 key-value pairs

get: operator that returns an item from a list at a specified index; also returns an value from a dictionary by specified key

len: operator that returns the number of items in a list

mutable: able to change

immutable: unable to change

set: replaces item in list at specified index; also, creates or replaces a key-value pair in a dictionary

append: adds items to end of a list, effectively increasing its length by one

reference variable: holds an address, not a value

equality: a value is equal to itself and to other values of same type and content

identity: a value in memory is identical only to itself

id: identity test operator

(id a b)   # returns true if a and b reference the very same object

Comments are closed.