Logo

Logo is an interpreted language. Logo phrases are commands, expressions or declarations:

PHRASE ::= CMMD | EXP | DEC

Executing an expression returns a value. Logo values include numbers, strings, lists, Booleans, agents, and agent sets.

Logo commands are executed by typing them to the Command Center prompt, or by associating them with GUI buttons.

Declarations

Declaring Variables

globals [VAR+] ; must appear at the top of a file
locals [VAR+]  ; first line of a procedure

Declaring Abstracts

There are two types of abstracts: procedures (programmer-declared commands) and functions (which return or report values):

to NAME [PARAM+]?
   locals [VAR+]
   CMMD+
end

to-report [PARAM+]?
   locals [VAR+]
   CMMD+
   report EXP
end

Expressions

Literals and variables

Function (and procedure) calls

Using parenthesis in function or procedure calls is optional in Logo. For example, the following are equivalent:

sin pi / 2
sin (pi / 2)
(sin pi / 2)
(sin (pi / 2))

Note that infix operators must have blank spaces on either side, otherwise Logo interprets them as parts of names.

Math

Operators: +, *, -, /, ^, <, >, =, !=, <=, >=

trig functions
log, ln, exp, sqrt
ceiling, round, floor, int, abs, max, min
lots of statistical functions
lost of random number generators
mod, remainder

Examples

to-report cube [n]
  report n * n * n
end

to-report tri [n]
  ;ifelse n = 0 [report 0] [report n + tri(n - 1)]
  report ifelse-value (n = 0) [0] [n + tri (n - 1)]
end

to-report fact [n]
  locals [i result]
  set result 1
  set i 1
  repeat n [set result result * i set i i + 1]
  report result
end

to-report even? [num]
   report num mod 2 = 0
end

Logic

CONDITION ::= true | false |
               not CONDITION |
               CONDITION (and | or | xor) CONDITION

Strings

String operators: +, <, >, =, !=, <=, >=

String Functions

item index STRING
reverse
substring
length

Example

to-report pallindrome? [msg]
   report msg = reverse msg
end

Lists

Selection: first, last, but-first, but-last, item, random-one-of

Predicates: length, empty?, is-list?, member?

Insertion: fput, lput, insert

Meta-functions: map, filter, sum, foreach

Constructors: list, sentence

Example: Sum of even squares

to-report soes [nums]
   report sum map [? * ?] filter [even? ?] nums
end

Example: Converting strings to lists of characters

to-report string2list [msg]
   locals [result pos]
   set result []
   set pos 0
   while [pos < length msg]
   [
     set result lput (item pos msg) result
     set pos pos + 1
   ]
   report result
end

Commands and Expressions (Reporters)

Assignment

set VAR EXP

I/O

print, read, show, type

Conditionals

if CONDITION [CMMD+]
ifelse CONDITION [CMMD+] [CMM+]
ifelse-value CONDITION [EXP] [EXP]

Iterations

foreach LIST [CMMD+]
loop [CMMD+] ; loops forever
repeat NUMBER [CMMD+]
while [CONDITION] [CMMD+]

Demonstration (from demos.nlogo)

globals [digits]

to init-globals
  set digits (list 0 1 2 3 4 5 6 7 8 9)
  clear-output ; clears command center
end

to demo
  output-demo
  list-demo
  math-demo
end

to output-demo
   print "Running output demo"
   print "Hello World" ; newline
   write "Hello World" ; quotes
   show "Hello World"  ; agent + newline
   type "Hello World"  ; no newline
end

to list-demo
  print "\nRunning list-demo"
  type "digits = "
  print digits
  type "cubes = "
  print map [cube ?] digits
  type "squares = "
  print map [? * ?] digits
  ;type "evens = "
  ;print foreach digits [ifelse-value (? remainder 2 = 0) ?] [0]
end

to math-demo
   print "\nRunning math-demo"
   type "fact 5 = "
   print fact 5
   type "tri 5 = "
   print tri 5
end

Building a GUI and running the demonstration

Next, create two buttons. Pushing the "init" button calls the init-globals procedure. Pushing the "run" button runs the demo procedure. Here's a screen shot of the output after furst pushing "init" and then pushing "run":

Running the demo from the Command Center prompt

Alternatively, any procedure could be called from the Command Center prompt. In fact any command can be typed at this prompt. To call a function at this prompt the user must embed it in a command. For example:

O> math-demo

Running math-demo
fact 5 = 120
tri 5 = 15
O> print fact 5
120