Sei sulla pagina 1di 8

INTRO TO COMPUTER SCIENCE STUDY GUIDE TERM 1

by Aaron Loo
SCHEME/RACKET
Arithmetic
Remember to put the operation FIRST
Correct: (+ 1 2)
Incorrect: (1 + 2)
There must be a space between each element, and there may be as many spaces as you please.
Correct: (* 2 5)
Incorrect: (*2 5)
Operations available:
+
*
/
expt (exponent, I.e (expt 2 3) = 2^3)
sqrt (square root, I.e (sqrt 4) = 2)
modulo (gives you the remainder of two #'s, I.e (modulo 21 2) = the remainder of 21/2)
quotient (gives you the quotient rounded down to the nearest whole number of 2 #'s, I.e
(quotient 21 2) 10)
abs (absolute value, I.e (abs -2) = 2
Compound Arithmetic
Scheme works from inside-out. Therefore:
(/ (* 3 5) (+ 2 3)) (/ 15 5) 3
Comparison Operations
Comparison operators return a true or false value
Like arithmetic operations, the operation is placed at the beginning.
I.e:

(< 11 20) #t
(< 21 20) #f

Comparison Operators:

>
<
=
>=
<=
THE EQUAL OPERATOR ONLY WORKS FOR NUMERIC COMPARISONS. FOR OTHER
PURPOSES, SUCH AS COMPARING TRUTH VALUES, USE EQUAL?, EXPLAINED
FURTHER BELOW.
THERE IS NO NOT EQUAL FUNCTION BUILT INTO SCHEME. INSTEAD, USE (NOT (=
A B)).
Logic Connectors

and (both linked clauses must be true for the whole statement to be true)
or (either linked clause can be true for the whole statement to be true)
not (reverse of the truth value of the statement)

I.e:
(and #t #t) #t
(and #t #f) #f
(or #f #t) #t
(or #f #f) #f
(not #f) #t
define
define can create either a variable or a function.
To create a variable:
(define -variable name- -value to be assigned-)
You may not use numbers for the variable name. You may use dashes
I.e:
(define seventyfour 74)
seventyfour 74
(define seventy-four 74)
seventy-four 74
To create a function:
A function is a variable that is assigned a task
There are two ways to create a function.

Basic:
(define (-function name- -dummy variables-) (-code-))
I.e:
(define (simple a b) (+ a b))
(simple 2 10) 12
Lambda (you only need to know how to read this):
(define -function name- (lambda (-dummy variables-) (-code-)))
I.e:
(define simple (lambda (a b) (+ a b)))
(simple 2 10) 12
Basic and Lambda are EXACTLY THE SAME THING.
if
if creates a conditional statement to be used in a function definition. It includes a test case, a true
part (the part of the code that will be executed if the test case is true), and a false part (the part of the
code that will be executed if the test case is false).
Example:
(define (If-Statement a b) (if (> a b) (+ a b) (/ a b)))
Analysis:
The function will test if the input a is greater than input b. If it is, then the two inputs will
be summed. If not, a will be divided by b.
cond
cond is a multi-faceted if statement. It will return numerous outputs under numerous test cases,
and will test each case in the order they are presented.
Example:
(define (Cond-Statement a b) (cond
((< a b) 13)
((= (+ a b) 7) -5)
((= (- a b) -2) (+ a b))))
Analysis:
If a is less than b, the function will return 13.
If the sum of a and b is equal to 7, the function will return -5.
If the difference of a and b is equal to -2, the function will return the sum of a and b.

Lists
Atom vs. List
Atom:

List:

7
+
32
alex
'(7)
'(+)
'(32 8)
'(alex and nicole)

Note: The apostrophe placed before a list means that Racket will not evaluate the list.
length
length measures the length of a list by its top-level elements
For example:
(length '(1 2 3 4 5)) 5
(length '(a b (c d (e) f) g)) 4
A top-level element is the number of atoms and lists there are in a list, and does not account for any
specifics within a sublist.
Stuff That Chops Up Lists
car and cdr are two commands that split a list into a smaller piece.
car takes a list, and returns its first element, be it an atom or a list.
For example:
(car '(a b c d e f g)) a
(car '((a b) c d e f g)) (a b)
cdr takes a list, and returns the list with the first element taken out.
For example:
(cdr '(a b c d e f g)) (b c d e f g)
(cdr '((a b) c d e f g)) (c d e f g)

If you take the car or cdr of an empty list, you will receive an error.
Compound car and cdr's work from the inside-out, much like arithmetic.
For example:
To retrieve the element never from the list '(I Will Never Die), you must:
Take the cdr of the list twice
Take the car to retrieve the element
As such, it will be formatted:
(car (cdr (cdr '(I Will Never Die))))
There is a shorthanded way to do this, and it is as follows:
(caddr '(I Will Never Die))
This short-hand will only be accepted up to four times.
(cadddr '(You Will Never Rise From The Ashes Of Your Humiliation)) Rise
(caddddr '(You Will Never Rise From The Ashes Of Your Humiliation)) ERROR
Stuffs That Makes Lists
Some commands create (bigger) lists. These commands are list, append, and cons
list accepts 0 or more arguments, and makes a list out of them.
For example:
(list ) ()
(list a b) (a b)
(list (a b) (c d)) ((a b) (c d))
append accepts one or more LISTS, and makes a bigger list out of the top level elements.
For example:
(append '(a b c)) (a b c)
(append '(a b) '(c d)) (a b c d)
(append '(a b c) d) ERROR
cons accepts exactly two arguments. The first can be either a list or an atom, but the second
argument MUST be a list. It tacks the second element onto the end of the first element.

For example:
(cons a '( b c)) (a b c)
(cons '(a) '(b c)) ((a) b c)
list?, null?, and equal?
These commands return a True and False value.
list? tests whether something is a list or not.
For example:
(list? '(a d)) #t
(list? 23) #f
null? tests whether a list is empty or not. It returns #t if the list is empty, and #f if it is not.
For example:
(null? '( )) #t
(null? '(a b d)) #f
equal? tests whether two or more elements being tested are of equal numerical or truth value, or if
they contain the same elements.
For example:
(equal? (< 2 3) (< 3 4)) #t
(equal? 3 3) #t
(equal? 3 0) #f
(equal? '(a b c) '(a b c)) #t
(equal? '(a b c) '(a d c)) #f
Recursive Functions
Recursive functions are functions that call itself over again until it reaches a base case. Generally, they
use if or cond, and the base case will be the true part or the else statement, respectively.
I.e,
(define (Recursive-Factorial n) (if (= n 1) 1 (* n (Recursive-Factorial (- n 1)))))
Analysis:
If the input of Recursive-Factorial is 1, then the output is simply 1. (base case)
If the input is greater than 1, then it will multiply n by every integer below it until it hits 1.

Example:
(Recursive-Factorial 5) 120
because
(Recursive-Factorial 5) makes the system do 5 * 4 * 3 * 2 * 1 = 120
NETLOGO*
* due to the breadth of NetLogo, many concepts are not covered in this sheet. As an alternative, refer to the NetLogo Dictionary,
or previous NetLogo works.

Basic Observer Commands

ca (clears the entire screen and resets all variables)


crt n (creates n number of turtles at the origin, facing random directions, and in random
colours.
cro n (creates n number of turtles at the origin, in order, in patterned colors.
cd (clears all drawings made by having turtles put their pen down on the screen)

In addition, all variables that are defined as globals are manipulated via Observer.
You cannot ask the observer to do anything, so ask observer [. . .] is NOT valid.
Basic Turtle Commands

fd n (asks the turtles to move forward n units. One unit is one patch.)
bk n (asks the turtles to move backwards n units. One unit is one patch.)
pd (asks turtles to put their pens down, thus allowing them to draw)
pu (asks turtles to put their pens up)
pe (asks turtles to put their erasers down)

All variables defined as turtles-own are manipulated via Turtles.


You can ask turtles from any other context by using ask turtles [. . .], or ask turtle -who number- [. . .]
to execute commands to a specific turtle.
For both crt and cro, you can place a set of brackets after the command to issue commands to that
particular set of turtles.
For example:
In observer context:
cro 15 [set shape face-sad set color red pd]
Analysis
It will create 15 ordered turtles
It will then ask those 15 turtles, and those 15 turtles only, to, in order:
set their shape to face sad

set their colour to red


put their pen down

Creating Command Sequences


All command sequences in NetLogo are formatted as such:
to -command name[. . . code . . .]
end
set
set will set the value of a certain variable to a numerical amount, or a true/false value. The context in
which set is used depends on the context of the variable whose amount is being set.
Turtle Properties
Include, but not limited to:
xcor
ycor
color
shape
label
hidden?
any property listed as turtles-own
Patch Properties
Include, but not limted to:
pxcor
pycor
pcolor
plabel
any property listed as patches-own
Creating Variables
Variables are created by placing globals [. . .], turtles-own [. . .], patches-own [. . .], etc. at the very top
of your code. Globals are in observer context, turtles-own are bound to turtle context, and patches are
bound to patches context.

Potrebbero piacerti anche