conditionals and flow
- = ==
- >
- <
- >=
- <=
- !=
- and
- case
- catch
- dosleep
- dountil
- dowhile
- else
- elsif
- eval
- every
- for
- foreach
- forever
- go
- gofrom
- if
- ifelse
- label
- loopabove
- loopcount
- oncefalse
- oncetrue
- onerror
- or
- otherwise
- output op
- pause
- repabove rpa
- repcount rct
- repeat rpt
- reptotal
- switch
- test
- throw
- try
- unless
- until
- when
- while
= ==
Takes: expression = expression
Returns: boolean
Returns 1 (true) if expression1 equals (has the same result) as expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets (). See equalp
if expression1 = expression2 [print [These are the same!]]
if (xpos + 4) = (ypos - 13) [...]
print = 1 2
0
>
Takes: expression > expression
Returns: boolean
Returns 1 (true) if expression1 is greater than expression2. Symbols may be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().
if :frogs > 0 [print "ribbit]
<
Takes: expression < expression
Returns: boolean
Returns 1 (true) if expression1 is less than expression2. Symbols may be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().
if :ships < 1 [gameover]
>=
Takes: expression >= expression
Returns: boolean
Returns 1 (true) if expression1 is greater than or equal to expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().
<=
Takes: expression <= expression
Returns: boolean
Returns 1 (true) if expression1 is less than or equal to expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().
!=
Takes: expression != expression
Returns: boolean
Returns 1 (true) if expression1 is not equal to expression2. May be placed before or between the two expressions to be compared. If the expression is complex (is a chain of primitives, procedures or mathematical operations), it needs to be surrounded by brackets ().
and
Takes: comparison1 comparison2 (comparison1 comparison2 comparison3…)
Returns: boolean
Returns 1 (true) if both the provided expressions are also true 0 (false) if not.
print and 5 = 4 3 = 2
0
if and 1 = 1 2 = 2 [print [Both are true!]]
Both are true!
if (and 1=1 2=2 3=3) [print [All are true!]]
All are true!
case
Takes: word, longword, number, boolean or list | list
Returns: none
Compares the input with the container specified by the previous switch primitive, and if true performs the contents of the list provided. See case, otherwise
case "yes [print |Welcome aboard!|]
catch
Takes: name | list
Returns: none
Catch executes the provided list. If throw <name> is called while the provided list is run, control returns to the first statement after the catch. If you provide “error as the name, catch catches errors instead.
catch "duck [repeat 100 [fd 20 throw "duck]] pr "quack
catch "error [repeat 100 [fd 20
dosleep
Takes: milliseconds (number) | list
Returns: none
Performs the provided list of instructions and then sleeps if and until the provided number of milliseconds has passed since the list started execution. If the first parameter is provided in the form of a single-item list, dosleep will instead attempt to average the given number of milliseconds taken to execute the contents of the second parameter such that if it takes longer than the provided number of milliseconds it will subtract that from the time allowed for the next iteration. See sleep, wait
dosleep 1000 [print "hello!]
dosleep [1000] [print |this attempts to average the execution speed of these instructions|]
dountil
Takes: expression | instructions (list)
Returns: none
Repeats the provided list of instructions until the provided expression is true.
make "counter 0 dountil :counter = 5 [make "counter :counter + 1 print :counter]
1
2
3
4
5
dowhile
Takes: expression | instructions (list)
Returns: none
Repeats the provided list of instructions while the provided expression is true.
make "counter 0 dowhile :counter < 5 [make "counter :counter + 1 print :counter]
1
2
3
4
5
else
Takes: list
Returns: none
Executes the provided list of requests if the previous if comparison was false. else is not necessary if the list of requests immediately follows the list of requests specified for the true condition, but else allows for the requests to be executed later in the program flow.
if :container = 5 [do this] [do that]
else is not needed here, since if can take an else request list
if :container = 5 [do this]
print |I am the walrus|
else [do that]
but else can come later
elsif
Takes: boolean | list
Returns: none
If the previous if statement is false, check the given boolean and if true, execute the given list. See if, else
if 1 = 2 [do this]
elsif 2 = 2 [do that]
eval
Takes: list
Returns: output
evaluates the expression in the supplied list and returns its output.
show eval [10 + 2]
12
every
Takes: counter (number) | count (number) | instructions [list]
Returns: none
Performs the list of supplied instructions every time the supplied count divides equally into the supplied counter. Shorthand for IF REMAINDER COUNTER COUNT []
every repcount 10 [print “moo]
for
Takes: [container start end (optional step)] (list) | [instructions to run] (list)
Returns: none
Repeats the list of requests counting from the start value to the end value, incrementing the value of :container by one (or the optionally provided step value) each time. The value stored in container remains after the for loop has finished executing.
for [i 1 15] [print :i]
1
2
3
...
for [i 1 10 2] [print :i]
1
3
5
...
foreach
Takes: container (word) | word or [list of items] | [list of requests]
Returns: none
Repeats the list of requests once for each item in the list of items while also changing the value of the specified container to the list item currently being processed. Note that the container name begins with a ” not a : when using foreach. Also, the value of container remains after the foreach loop has finished executing.
foreach "animal [goose horse sheep] [print {[Old Macdonald had a] :animal [E I E I O]}]
foreach "char "duck [print :char]
forever
Takes: list of requests
Returns: none
Repeats the list of requests until terminated by another thread or halt.
forever [print [I am slowly going crazy 1 2 3 4 5 6 switch]]
go
Takes: word
Returns: none
Jumps to the execution point marked by label with the specified word. Similar to the BASIC GOTO command. Should be avoided if at all possible. See label.
label "start
print |This will repeat forever!|
go "start
gofrom
Takes: procedure (word) | label (word)
Returns: none
Executes the specified procedure starting from the specified label. See go, label
gofrom "myproc "label
if
Takes: expression | list
Returns: none
if can act both as an advisor (function) or a performer (command). If acting as a thinker and the provided expression resolves true, if returns the provided list. If acting as a doer and the provided expression resolves true, if executes the instructions in the provided list. See unless, which is an inverse if
if 4 = 4 [forward 20]
show if 4 = 4 ["duck]
ifelse
Takes: expression | list | list
Returns: none
ifelse can act as both an advisor or a performer. If the provided expression resolves true, ifelse (advisor) returns the first list, otherwise it returns the second list. If ifelse is acting as a performer, it executes either the first list or the second. See if, else
ifelse :container = 5 [do this] [do that]
show ifelse :container = 5 ["this] ["that]
label
Takes: name (word)
Returns: none
Creates a ‘label’ with the specified name, which can be ‘jumped’ back to during execution using go. See go
label "start
go "start
loopabove
Takes: level (number)
Returns: number
Returns the loopcount of the specified level above the current loop. See loopcount, repabove
forever [repeat 5 [show loopabove 1]]
loopcount
Takes: none
Returns: number
Returns the current repetition of a forever, while, until, dowhile, dountil, repeat or foreach loop. See repcount which only works for repeats
forever [show loopcount]
oncefalse
Takes: boolean | instructions (list)
Returns: none
Preforms the provided list of instructions if the boolean is false once, and not again until the boolean becomes true and then false once more (for example, when the turtle crosses a boundary). To ‘flip’, oncefalse must see the boolean become true (be called when the boolean is true). See oncetrue
oncefalse :boolean [instructions]
oncetrue
Takes: boolean | instructions (list)
Returns: none
Preforms the provided list of instructions if the boolean is true once, and not again until the boolean becomes false and then true once more (for example, when the turtle crosses a boundary). To ‘flip’, oncetrue must see the boolean become false (be called when the boolean is false). See oncefalse
oncetrue :boolean [instructions]
onerror
Takes: list
Returns: none
Causes the calling turtle to execute the supplied list of instructions when an error is encountered rather than stop execution. It then continues execution from the point the error was encountered. onerror is ‘scoped’ to the procedure it is called from.
onerror [print "Caught!] show 1 / 0
Caught!
or
Takes: expression | expression
Returns: boolean
Returns true or false based on whether or not one or more of the supplied expressions are true. or takes two expressions by default, but can take more than two if the entire or statement is wrapped in brackets, eg: (or expr1 expr2 expr3 …) See and
show or 1 = 1 2 = 3
TRUE
otherwise
Takes: list
Returns: none
If no case statement has been satisfied since the last switch, execute the provided list. See switch, case
otherwise [pr |Eat a sandwich.|]
output op
Takes: input
Returns: output
Used by procedures to return output to a calling procedure or function. The input and output are effectively the same.
to duck
output "quack
end
print duck
quack
pause
Takes: none
Returns: none
Causes execution in the calling worker to pause and return to the prompt. Execution can be continued with co. Pause only works with the main (archon) worker. NOTE: pause DOES NOT WORK in interactive mode (only in a procedure!) See co
pause
repabove rpa
Takes: number
Returns: number
Returns the current iteration of a repeat loop at the specified level above the current repeat loop. For example, repabove 1 returns the repcount value of the repeat loop directly (one level) above the current repeat loop. If the repeat ‘scope’ specified by repabove does not exist, repabove returns 0. See repcount
show repabove 1
repcount rct
Takes: none
Returns: number
Returns the current iteration of a repeat loop. See repabove, loopcount
repeat 10 [print repcount]
repeat rpt
Takes: repetitions [list of requests to repeat]
Returns: none
Repeats the list of requests specified in square brackets the declared number of repetitions. See repcount
repeat 4 [forward 10 right 90]
reptotal
Takes: none
Returns: number
Returns the target number of loops inside a repeat. See repeat, repcount
repeat 10 [show reptotal]
switch
Takes: containername (word) or list
Returns: none
If the name of a container is given, switch sets up the specified container to be tested using subsequent case primitives. Note: the container name for switch is ‘absolute’, that is, prefaced with a double quote “. If you specify a container using a colon : the container used by switch will be the container named by the container specified with a colon! However, if a list is given, subsequent case statements will test against the first item in the list. Hence, you can use a ‘softlist’ to test against the value stored in a container or returned by a calculator.
switch "var
switch [testme donttestme]
switch {:var}
test
Takes: expression
Returns: none
Evaluates the provided expression and then holds a boolean result for use with iftrue and iffalse.
test :a > 5
throw
Takes: target (name)
Returns: none
Diverts execution to the first statement after the target catch statement. Chiefly useful for returning to the procedure / level the catch was called from when several levels deep into subprocedures or other procedures, as label / go only work inside the same procedure. Providing “toplevel as a target effectively halts execution, as toplevel is the interactive prompt. See also catch, label, go
TO test
catch "cow [duck]
pr "moo
END
TO duck
pr "quack
frog
END
TO frog
pr "ribbit
throw "cow
END
try
Takes: list | list
Returns: none
Attempts to execute the instructions given in the first list, and if an error is encountered then executes the instructions in the second list. A one-off form of onerror. See onerror, throw, catch
try [repeat 10 [show 10 / (10 - repcount)]] [print |Divide by zero, silly!|]
unless
Takes: expression | list
Returns: none
Performs the provided list of commands if the expression is false. This is the inverse behavior of if. See if
unless 3 = 4 [forward 20]
show unless 3 = 4 ["duck]
until
Takes: comparison [list of requests]
Returns: none
Repeats the list of requests until the comparison is true.
make "count 1 until :count = 20 [inc "count]
when
Takes: comparison [requests]
Returns: none
Waits until the comparison is true, then executes the provided requests. This blocks execution; it is not a trigger. To use it as such, use newworker eg newworker [when minutes = 0 [toot 1000 60]]
when minutes = 0 [toot 1000 60]
while
Takes: comparison [instructions]
Returns: none
While the provided comparison is true, the supplied instructions will be repeatedly executed.
while :x < 10 [make "x :x + 1 print :x]