You are on page 1of 2

NetLogo Column 1 NetLogo Column 2

crt n create n turtles (random headings)


if condition if the condition is true, then execute the commands
create n turtles (equally distributed [commands]
cro n headings)
ca clear all
ifelse condition
if the condition is true, then execute commands-1,
cp clear patches [commands-1] otherwise execute commands-2
[commands-2]
cd clear drawing

while the test is true, repeatedly do the commands.

move the turtle(s) to xcor = new-x while [test] while [ xcor < 0 ]
setxy new-x new-y and ycor = new-y [ fd 1
[ commands ]
set pcolor green
]
fd n forward n steps
calculates the distance between the turtle (or patch) and the
distancexy xvalue yvalue point (xvlaue, yvalue)
bk n backward n steps
calculates the square root of the expression - mouse-xcor and mouse-ycor are the
sqrt expression (e.g. coordinates (current position) of the
sqrt (xcor * xcor + ycor * mouse
mouse-xcor and mouse-ycor
ycor)) will calculate the distance of this - mouse-down? is true if the mouse
turtle from the origin
mouse-down? button is pressed, false otherwise
mouse-inside? - mouse-inside? is true if the mouse
a mod b calculates the remainder when a is divided cursor is inside the NetLogo visual
by b. e.g. 13 mod 5 is 3 area, false ptherwise

rt n rotate right n degrees let variable1 value1 create variables used only in the current procedure
create variables seen and modifiable throughout the
lt n rotate left n degrees globals [global-variable-1 ...] program
pu pen up turtles-own [property-1 ...] create properties for turtles
pd pen down (draw) patches-own [property-1 ...] create properties for patches
to procedure-name
set size n change size of turtle ... define a procedure
end
to-report reporter-name
set color n
...
(or) change color of turtle define a reporting procedure
report expression
set color color-word
end
who, xcor, ycor, color, shape, size, heading, label, label-
repeat n [ ] repeat n times the commands in [ ] Common properties of a turtle color, pen-size, pen-mode, hidden?, breed
set shape "shape change shape of turtle Common properties of a patch pxcor, pycor, pcolor, plabel, plabel-color
name"
"forever" button continuously submits its commands

random n returns (reports) a number between 0 and n Idioms


set the color of turtle 12 to a random ask turtle 12 [set color random 140]
set pcolor n sets the color of the patch
value
to wiggle [stepsize angle]
rt random angle
paints the ground underneath a turtle with
stamp the image of the turtle create a "wiggling" procedure lt random angle
fd stepsize
end
sample of a "collision" procedure:
set fred [-8 3 “harry”] to overcrowding-check
set label item 2 fred if count other turtles-here >= 2
lists and list functions set shape one-of [“cow” “wolf” “ant”] if there are 3 or more turtles on a [ask turtles-here [die]]
set fred lput “harry2” fred patch, make them die from end
overcrowding
print sum [pcolor] of patches
if [xcor] of turtle 0 > 0 [ … ]
summary functions (sum, max, min, if max ([xcor] of turtles) > 8 [ … ]
“of” ask one-of turtles with [xcor > 0] [die]
print mean [color] of turtles
ask min-one-of turtles [xcor] [die] mean, median, etc.)

Game of Life Rules


...then in
...and the number
the next
If the cell is... of its living ...because...
generation
neighbors is...
it will be...
it's
Alive 2 or 3 alive
comfortable
Alive less than 2 dead loneliness
Alive more than 3 dead overcrowding
it takes 3
Dead 3 alive
parents
less than or greater
Dead dead that's Life
than 3
Scheme IDEA EXPLANATION Scheme EXAMPLES
lists are formed by enclosing a sequence of items (atoms or
lists (12 Harry (another list))
lists) inside opening and closing parentheses
Scheme will try to interpret the first element of a list as a (+ 1 2)
evaluation function to perform, and the rest of the elements as arguments (cdr L)
to that function. (if (= n 2) 12 18)
Quoting a list using the apostrophe (') will prevent Scheme
preventing evaluation (cdr '(do not evaluate)) -> (not evaluate)
from evaluating a list
"define" will create a memory location with the name of the (define a 12) -> a=12
defining variables first argument, and store the value of the second argument in (define Harry (+ 10 2)) -> Harry=12
that memory location (define L '(+ 10 2)) -> L=(+ 10 2)
The ordinary 4 arithmetic functions will work with both (+ 1 2) -> 3 (+ 1 2 3) -> 6
whole numbers and decimals and fractions, and will usually (- 5 3) -> 2 (- 5 3 1) -> 1
arithmetic operations take more that 2 arguments (* 5 2) -> 10 (* 5 2 3) -> 30
(/ 8 2) -> 4 (/ 7 2) -> 3 ½
abs is the absolute value function. (abs -3) -> 3 (abs -2.3) -> 2.3
modulo returns the remainder of its first argument divided by
integer arithmetic its second argument. (modulo 13 5) -> 3
operations quotient divides its first argument by its second, and ignores (quotient 13 5) -> 2
the remainder
(+ (* 3 4) 2) -> 14
compound
Lists within lists are evaluated from inside to outside (modulo (quotient 543 10) 10) -> 4
expressions
(quotient (modulo 543 10) 10) -> 0
Functions can be defined with one or more arguments. The
functions (without (define (square n) (* n n))
argument names can be arbitrarily chosen, as long as they
lambda) (define (square bleep) (* bleep bleep))
match the use of that argument inside the function body.
Another form of function definitions uses the Greek letter
functions (with lambda. (define square ( (n) (* n n)))
lambda) Note: in the current Windows version of DrScheme (299.x), (define square (lambda (n) (* n n)))
use the word "lambda" instead of the symbol “”..
(= 12 13) -> #f
(> 4 2) -> #t
relational operators These operators compare numerical values and return #t or #f (>= 4 (+ 1 3)) -> #t
(< 1 2 3) -> #t
(<= 1 2 0) -> #f
(and (= 4 4) (> 4 5)) -> #f
"and", "or" and "not" combine several logical expressions
conjunctions (or (= 4 4) (> 4 5)) -> #t
into larger logical (boolean) expressions
(not (= 4 4)) -> #f
Make decisions with "if". An if-list always has 4 elements:
(if (test-p (if (= 12 13) 14 (+ 1 2)) -> 3
if
The "if-list" then returns the value of its true-part or its false- (if (< 12 14) 14 (+ 1 2)) -> 14
part.
Multi-decision lists can be composed with "cond", which has
(cond
any number of clauses, each with a test and a result. The
( (> grade 100) "Extra credit")
clauses' tests are evaluated one after the other until one of the
( (and (>= grade 65) (<= grade 100)) "Pass")
cond tests is true, then that clause's result is immediately returned
( else "Fail")
as the "cond" list's value. An "else" clause may be included
)
as the last clause and will be activated if all of the other
clauses's tests are false.
(define (PrintGrade num)
(cond
((> num 100) "Extra credit")
functions with
((and (>= num 65) (<= num 100)) "Pass")
decisions
(else "Fail")
)
)
(define L '(Harry (Tom Riddle) Ron))

(car L) -> Harry


(cdr L) -> ((Tom Riddle) Ron)
- "car" returns the first element of a list
- "cdr" returns the rest of a list once the first element has been (car (cdr L)) -> (Tom Riddle)
removed (car (car (cdr L))) -> Tom
list processing - "cons" creates a larger list whose first element was cons's (cdr (cdr L)) -> (Ron)
functions first argument, and the rest was cons's second argument
- "list" creates a list from its arguments (cons 'Fred L) -> (Fred Harry (Tom Riddle) Ron)
- "append" combines all of its arguments (which must be (cons (car L) (cdr L)) -> L
lists) into one larger list
(list 1 2 3) -> (1 2 3)

(append '(Fred) L) -> (Fred Harry (Tom Riddle)


Ron)
abbreviations of One can abbreviate up to 4 sets of these functions with a (car (cdr (cdr (cdr L))))
sequences of car and single function whose letters are formed from the middle can be abbreviated:
cdr letters of the sequence of car and cdr. For instance: (cadddr L)
(define (mylength L)
A recursive function may make a call to itself during (if (null? L)
evaluation. When designing a recursive function, make sure 0
that you include a base case which does not call the function (+ 1 (mylength (cdr L))))
recursive functions
itself. The typical recursive function tries to reduce a large (define (factorial n)
problem to a smaller problem, and then call itself with the (if (< n 2)
smaller problem, until the base case is reached. 1
(* n (factorial (- n 1)))))
- (null? L) returns #t is the list is empty, else #f (null? '()) -> #t
- (list? P) returns #t if P is a list, else #f (list? '(a b)) -> #t (list? 'a) -> #f
- (sqrt n) returns the square-root of the number (sqrt 2) -> 1.4142135623730951
miscellaneous
- (min a1 a2 a3 …) returns the minimum of arguments (min 5 3 9) -> 3
functions
- (max a1 a2 a3 …) return the maximum of arguments (max 5 3 9) -> 9
- (even? n) return #t if the N is even, else #f (even? 3) -> #f
- (equal? a b) returns #t if a and b are equal, else #f (equal? '(a b) '(a b)) -> #t

You might also like