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.
globals [VAR+] ; must appear at the top of a file
locals [VAR+] ; first line of a
procedure
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
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.
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
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
CONDITION ::= true | false |
not CONDITION |
CONDITION (and | or | xor)
CONDITION
String operators: +, <, >, =, !=, <=, >=
item index STRING
reverse
substring
length
to-report pallindrome? [msg]
report msg = reverse msg
end
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
to-report soes [nums]
report sum map [? * ?] filter [even?
?] nums
end
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
set VAR EXP
print, read, show, type
if CONDITION [CMMD+]
ifelse CONDITION [CMMD+] [CMM+]
ifelse-value CONDITION [EXP] [EXP]
foreach LIST [CMMD+]
loop [CMMD+] ; loops forever
repeat NUMBER [CMMD+]
while [CONDITION] [CMMD+]
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
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":
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