Logo for NXT Reference Guide
Version: July 25th 2009
Return to main page
See also: User Guide and nxt.imt reference.
Contents:
|
|
Legend for expected/returned arguments:
|
n |
number |
|
w |
word |
|
l |
list |
|
v |
any value |
|
wl |
word or list |
|
ni |
number or number interval [from to] |
|
- |
does not return value |
|
Basics
NXT Logo is easy to learn and use. It is derived from Imagine Logo with
small differences due to implementation
and efficiency reasons, so a user of any Logo language can start using it immediately.
Logo is a general-purpose language with list data structures (lists
resemble stack-like data structures: you can obtain/insert/remove the first element of the list in
an atomic operation).
Logo is derived from the functional programming language Lisp. In functional languages, the code
(functions) are also considered to be data, i.e. you can manipulate with the functions and procedures as if they
were data (for example pass them to other functions in arguments, modify them, etc.), and the data can
be interpreted as executable code, i.e. your program can construct a piece of code and then run it immediately.
Thus NXT Logo has to be an interpreted language and the simplest way it interacts with the user is through an
interactive command line: NXT Logo waits for a user to type in the next list-expression to be evaluated,
and then it returns the resulting value.
NXT Logo works with values of three different types: numbers (1, -5, 100), words (hello, a, x),
and lists that are enclosed in square brackets and can be nested
([a b c], [1 2 3], [[this is] a [[very [nested [list]]]]]). NXT Logo currently supports only 14-bit signed
integers, i.e. numbers in the range [-8191,8191].
The evaluation is driven by simple rules:
- numbers are returned directly, i.e. 42 —> 42
- lists are returned directly, i.e. [1 2 x z] —> [1 2 x z]
- words (symbols) are names of procedures/functions, and the arguments are supplied in prefix notation, i.e.
add 1 3 —> 4. A useful feature to notice is that the arguments to procedures/functions should comply
with one of these rules, and that they are (obviously) evaluated before the procedure/function is called, i.e.
add sub 3 2 1 —> 2, because (3 - 2) + 1 = 2.
However, words can be prevented from being evaluated as procedures, if they are preceded with double quotes, i.e.
"hello —> hello. And finally, words can represent variables that can hold values of any type.
The value of a variable is obtained by prepending its name with the column sign, i.e.
:x —> value of variable x.
Variables are assigned values using make, let, and when they appear in the argument list of
a user-defined procedure/function, or using the foreach procedure, see below.
Note: if the input list-expression consists of several sub-expressions, all of them are evaluated and the
value returned is equal to the return value of the first sub-expression. For example the expression:
gt? 7 5 make "x 2 print :x —> "true
contains three sub-expressions: calling of the predicate function gt? that returns true, assingning
the value to variable x, and printing the value of variable x.
Users can define their own procedures/functions using to as follows:
to "procedure_name list_of_arguments [cmd cmd ...]
for example, the following function computes the sum of all elements in a list:
to "sum [:lst]
[
let "s 0
foreach "e :lst [make "s add :s :e]
op :s
]
Note that the formal arguments in the list of arguments must start with the column sign (in this case :lst),
and that the name
of the procedure/function must start with a double-quote character (obviously, otherwise it would be attempted
to be called before the to is called, in this case "sum). The procedures/functions have a fixed number
of arguments (but of course, you can combine variable number of arguments into a single list-argument). Also note
that the keyword op (abbreviation of 'output') is used to return a value from a function.
op can occur at more than one place in a function or not at all, in which case it is a procedure,
i.e. a function that does not return a value. Use the keyword stop to return from anywhere inside
of a procedure. In usual Logo languages, it is common that some functions/procedures have two names: a short,
abbreviated name, and the long name (for instance fd and forward). In NXT Logo, the synonyms
are not defined by default (although you may define them yourself).
NXT Logo provides many pre-defined procedures and functions, which are all described here.
Arithmetic operations
If the argument(s) of the arithmetic operations described below are not number(s), an exception is thrown.
When an exception is thrown, the execution of the program stops, control is returned
to the top level, and word "E is returned. The details of the exception can be obtained
by calling getexception command.
neg
n
—> n
Negative of the argument.
Example:
neg 4 —> -4
add
n1 n2
—> n
Sum the two arguments.
Example:
add 10 -3 —> 7
sub
n1 n2
—> n
Subtract the second from the first.
Example:
sub 10 -2 —> 12
mul
n1 n2
—> n
Multiply the two arguments.
Example:
mul 10 -2 —> -20
div
n1 n2
—> n
Divide the first by the second.
Example:
div 10 -2 —> -5
mod
n1 n2
—> n
Remainder of division of first by second.
Example:
mod 15 7 —> 1
Logical operations
Logical operations accept words "true and "false, however, they also
accept numbers, where 0 represents "false and everything else represents
"true. Passing other words, or list arguments to logical operations results
in exception.
and
w1 w2
—> w
Logical and.
Example:
and "true "false —> false
and 2 3 —> true
or
w1 w2
—> w
Logical or.
Example:
or "true "false —> true
or 2 0 —> true
xor
w1 w2
—> w
Logical xor.
Example:
xor "true "false —> true
xor 2 3 —> false
not
w
—> w
Logical not.
Example:
not "true —> false
not 0 —> true
Bitwise logical operations
Bitwise logical operations work with numeric arguments. Passing words or lists results in exception.
bitand
n1 n2
—> n
Bitwise and.
Example:
bitand 37 54 —> 36
bitor
n1 n2
—> n
Bitwise or.
Example:
bitor 37 54 —> 55
bitxor
n1 n2
—> n
Bitwise xor.
Example:
bitxor 37 54 —> 19
Operations on lists and words
Lists and words have the same structure: they consist of the first element and the rest of the word or list.
Rest of one-element list is empty list [] and rest of one-character word is empty word ".
Therefore some structural operations can be used on both words and lists. In fact, they also work
on numbers. The words can contain any characters with ascii values 0-255 (i.e. they can contain binary data),
their length is limited only by the available memory size. Likewise, the length of lists is not limited.
first
wl
—> v
The first element of list/word.
Example:
first [37 54 34 21 12] —> 37
first "hello —> h
first 321 —> 3
last
wl
—> v
The last element of list/word.
Example:
last [37 54 34 21 12] —> 12
last "hello —> o
last 123 —> 3
bf
wl
—> v
List/word without the first element.
Example:
bf [37 54 34 21 12] —> [54 34 21 12]
bf "hello —> ello
bf 231 —> 31
bl
wl
—> v
List/word without the last element.
Example:
bl [37 54 34 21 12] —> [37 54 34 21]
bl "ola —> ol
bl 789 —> 78
count
wl
—> n
Returns the number of elements of a list, or number of characters of a word.
Example:
count [37 54 34 21 12] —> 5
count "ola —> 3
count 1234 —> 4
item
ni wl
—> v
Return specified element of list or word. The first element has index 1. If the element index is out of range,
item returns empty list or word.
Coming soon: specifying interval in the first argument returns sublist/subword.
Example:
item 3 [37 54 34 21 12] —> 34
item 2 "hello —> 2
item 3 8123 —> 2
item 3 -8124 —> 1
fput
v1 lw
—> lw
Inserts an element in front of a list or a word. Returns the resulting list or a word.
Example:
fput "a [1 2 3] —> [a 1 2 3]
fput "a "xyz —> axyz
fput [a] [1 2 3] —> [[a] 1 2 3]
lput
v1 lw
—> lw
Inserts an element behind a list or a word. Returns the resulting list or a word.
Example:
lput "a [1 2 3] —> [1 2 3 a]
lput "a "xyz —> xyza
lput [a] [1 2 3] —> [1 2 3 [a]]
se
v1 v2
—> l
Wraps both values in lists (if they are not lists yet), and then appends the two lists.
Example:
se [1 2 3] [7 8 9] —> [1 2 3 7 8 9]
se [3 4 5] "hello —> [3 4 5 hello]
se 34 "x —> [34 x]
se "a "b —> [a b]
list
v1 v2
—> l
Returns list with the two specified elements.
Example:
list 3 [4 5 6] —> [3 [4 5 6]]
list "a "b —> [a b]
word
v1 v2
—> w
Converts both values to words and then appends them.
Example:
word "ab "c —> abc
word "x 11 —> x11
word [a b c] 14 —> a b c14 (which is a word)
word "a -8124 —> a-8124
Operations and procedures for iterations
map
wl l
—> l/-
Applies the specified procedure on every element of the second list.
If the specified procedure is operation - i.e. it returns value, map
will return the list of values returned by the function in individual
applications. The procedure must take 1 argument.
Example:
map "first [word 123 [a b c d] [[4 5] [x y]] -23] —> [w 1 a [4 5] -]
apply
wl l
—> v
Runs the specified procedure with the arguments specified in the second list.
Example:
apply "add se first [40 20] item 2 [40 30 20] —> 70
apply "first [[1]] —> 1
foreach
w wl l
—> -
Runs the third list-expression for each element of the second
list/word assigned to variable specified as the first element. Foreach does not return value.
Example:
make "l []
foreach "var [100 102 104] [make "l fput mod :var 7 :l]
:l —> [6 4 2]
run
l
—> -/v
Runs the specified list-epxression, optionally returns value.
Example:
make "x 7
run se [:x make "l] :x —> 7
repeat
n l
—> -
Runs second list-expression n-times. You can use the repc function
inside of repeat body to determine the repetition number (starting with 1).
Repeat does not return a value.
Example:
make "x []
repeat 10 [make "x fput repc :x]
:x —> [10 9 8 7 6 5 4 3 2 1]
while
[v] l
—> -
While the first argument evaluates to "true (or non-zero number), runs the list-expression provided in the
second argument. While does not return a value.
Example:
while [gt? sensor 1 50] [tone 220 100 2 wait 200]
try
l1 l2
—> -/v
Runs the specified list-epxression. If exception occurs while the list is being evaluated, it is
caught, and the second list is evaluated. If no exception occurs, try returns the value returned
by the first expression. If exception is caught, try returns the value returned by the second expression.
Example:
try [:x] [print [variable x probably does not exist]]
Conditions and predicates
if
v l
—> -/v
If v evaluates to "true or non-zero number,
runs the list-expression and returns the value returned by the list-expression (if any).
Example:
if gt? sensor 1 50 [motor 1 "onfwd 50]
ifelse
v l1 l2
—> -/v
If v evaluates to "true or non-zero number, runs the first list-expression,
otherwise runs the second list-expression; if the selected list-expression returns value, ifelse returns that value.
Example:
ifelse gt? sensor 1 50 [motor 1 "onfwd 50][motor 2 "onfwd 50]
empty?
v
—> w
Returns true, if value is empty word or empty list, otherwise returns false.
Example:
empty? [1 2 3] —> false
list?
v
—> w
Returns true, if value is a list, otherwise returns false.
Example:
list? "hello —> false
word?
v
—> w
Returns true, if value is a word or number, otherwise returns false.
Example:
word? "hello —> true
number?
v
—> w
Returns true, if the value is a number, otherwise returns false.
Example:
number? 42 —> true
eq?
v v
—> w
Returns true, if the two specified values are the same. In case of lists, eq?
compares "pointers". I.e. two different lists with the same contents are not "eq", only if one
compares a list with another reference to the very same list.
Example:
eq? 42 4242 —> false
eq? "hello "hello —> true
eq? [1 2 3] [1 2 3] —> false
make "x [1 2 3 4]
make "y fput "a bf :x
eq? bf :x bf :y —> true
gt?
n n
—> w
Returns true, if the first number is greater than the second number, or
if the second word alphabetically precedes the first word, otherwise returns false.
Example:
gt? 42 4242 —> false
lt?
n n
—> w
Returns true, if the first number is lower than the second, or if the first word alphabetically precedes the second word, otherwise returns false.
Example:
lt? 42 4242 —> true
ge?
n n
—> w
Returns true, if the first number/word is greater or equal than the second, otherwise returns "false
Example:
ge? 42 42 —> true
le?
n n
—> w
Returns true, if first number/word is lower or equal than the second, otherwise returns false.
Example:
le? 42 42 —> true
Working with variables and procedures
make
w v
—> -
Assigns value to specified variable - local or global, if variable does not exist yet, creates a global one.
make does not return a value.
Example:
make "x [1 2 3]
first :x —> 1
let
w v
—> -
Assigns value to specified local variable, if variable does not exist yet, creates one.
Note: when evaluating expressions at top level (in global context), the global variables are considered to be local.
let does not return a value.
Example:
make "x [1 2 3]
to "f [] [let "x sum :x op :x]
f —> 6
:x —> [1 2 3]
note: sum is defined above.
to
w l1 l2
—> -
Defines a procedure with given name, list of arguments, and body. Note that to is used slightly differently
than in Imagine. to does not return a value.
Example:
to "fib [:n]
[
op fib2 1 1
]
to "fib2 [:a :b]
[
if le? :n 1 [op :b]
make "n sub :n 1
op fib2 add :a :b :a
]
fib 6 —> 8
text
w
—> l
Returns the definition list of a user-defined function/procedure. Supplying the system
procedure results in exception. If no procedure is defined for the supplied word, empty
list is returned. The definition list is a list with two elements, where first element
is a list of columned arguments, and the second element is the body of the procedure.
to "pow [:a :b]
[
if eq? :b 0 [op 1]
op mul :a pow :a sub :b 1
]
text "pow —> [[:a :b][if eq? :b 0 [op 1] op mul :a pow :a sub :b 1]]
erase
w
—> -
Erase variable or procedure. To erase procedure, specify its name.
To erase variable specify its name preceded by column. Erase does not return a value.
Example:
erase "fib
erase ":fib
op
v
—> -
Outputs value from the current procedure/function. The evaluation of the currently evaluated expression
and currently evaluated function body stops immediately, and the specified value becomes the return
value of the function/procedure that is currently being evaluated. If the evaluation is at the top level,
evaluation stops and an exception is thrown.
Example:
to "gcd [:a :b]
[
if gt? :b :a [op gcd :b :a]
if eq? :b 0 [op :a]
op gcd :b mod :a :b
]
stop
v
—> -
Return from procedure. The evaluation of the currently evaluated expression
and currently evaluated procedure body stops immediately, while no value is returned.
If the evaluation is at the top level, evaluation stops and an exception is thrown.
Example:
to "beeper []
[
repeat 100
[
if eq? sensor 1 1 [stop]
tone 1760 100 3
wait 150
]
]
Other procedures and operations
load
w
—> w
Load specified file from NXT flash and interpret all commands in it. If loading succeeds, true is
returned. Otherwise, false is returned.
Example:
load "follow.lgo
wait
n
—> -
Wait the specified number of milliseconds. Wait does not return a value.
Example:
Wait 1000
random
n
—> n
Returns a random number from interval [0,(n-1)].
Example:
random 2 - returns 0 or 1
gc (not implemented yet, coming soon)
n
—> -
Do garbage collection, return latest after n milliseconds, even if not finished.
Garbage collection is started automatically, when all memory is occupied. See also the fm function below.
gc does not return a value.
Example:
gc 300
fm
—> n
Return the amount of free memory in double-words (i.e. number of free bytes / 4). The memory is organized
in 16-bit words, i.e. one number, symbolic word, or list element normally occupies one 16-bit word.
Of course, some words are used for overhead, and when a list is allocated, extra slots are allocated
to allow for the list to grow without the need to allocate new memory block in order to decrease the overhead.
You can regulate the number of these extra words using the system procedure.
Example:
fm —> amount of free memory
remote (not implemented yet)
v
—> v
Evaluates the specified expression on a remote Imagine Logo interpreter running on PC. The execution
pauses until a response arrives back. Note that the Bluetooth technology does not allow fast switching
of the communication direction, and therefore you must expect an extra delay (roughly 150ms for each
request). Note that the expression that is remotely evaluated in the Imagine Logo may not request
another remote evaluation on NXT before returning. Also note that if the NXT program was started
manually using the NXT buttons (instead of remotely from Imagine Logo), the remote evaluation is not
available.
Example:
remote [load_and_return_settings]
ascii
w
—> n
Returns ascii-value of the first char in word w.
Example:
ascii "hello —> 104
char
n
—> w
Returns a one-character word with character of the specified ascii value.
Example:
char 104 —> h
getexception
n
—> n
Returns the exception number. The list of exceptions is listed in the beginning of the file kernel.nbc.
If no exception occured, the returned value is random.
Example:
try [f] [print se [Exception occured:] getexception]
system
n v
—> v/-
System command. This advanced command can be used to configure NXT Logo internals.
The first argument specifies the command, while the second argument is its argument.
The following system commands are currently available:
command
0
1
2
3
4
5
6
|
argument
(ignored)
value
pointer to expression
breakpoint number
(ignored)
number of elements
number of elements
|
meaning
dump core - file core.dat with the memory map
will be saved immediately to NXT flash.
It can be printed using the catcore
win32 utility. No return value.
set debug flag - sets the internal system_debug_flag
variable to specified value. It can be
used when debugging the interpreter.
No return value.
set breakpoint - (not implemented yet), this will
add a breakpoint - each time the specified
expression will be evaluated, the evaluation
will be paused, and Imagine Logo will be
notified using BlueTooth packet, the execution
will resume when Imagine Logo will send
"continue" packet. Returns breakpoint number.
clear breakpoint - (not implemented yet), this deletes
the specified breakpoint. No return value.
clear all breakpoints - (not implemented yet), this
deletes all breakpoints. No return value.
minimum list extra space - set the minimum number of
extra list elements when new list is allocated
(default is 4)
maximum list extra space - set the maximum number of
extra list elements when new list is allocated
(default is 10)
|
Example:
system 0 0
NXT file system
fopen
w1 w2 n
—> l
Opens the specified file in the flash of NXT for specified mode (r - read,
w - write ,a - append)
and the file size (specify 0 for read mode). Returns list [handle size].
The files in the NXT flash use the 15.3 notation, i.e. max. 15 characters for the
name and 3 characters for extension. When opening file for writing,
the exact resulting file size must be provided. However, you can close the
file before writing all data. The file system will remember the planned total
file size as well as the current file size. You can then reopen the file
with the append mode, and continue writing (I believe this is the only way
you can use append - the NXT file system does not allow appending to a file
that is already fully written). If fopen fails, the handle returned is -1,
otherwise it is non-negative. When opening for writing, an existing file
is automatically deleted before opening (internally using FileDelete
NBC syscall).
Example:
make "handle first fopen "data.txt "w 100
fwrite
n w
—> n
Writes the word w to file specified with handle n.
If write completes corretly, returns 0, otherwise returns error code.
Example:
fwrite :handle "hello
fread
n1 n2
—> w
Reads n bytes (second arg.) from the file specified with handle (first argument).
Returns a word consisting of the read characters. If reading fails, empty word is returned.
Example:
fread :handle 5 —> hello
fclose
n
—> n
Closes a file specified with handle, returns 0, if successful, or error code otherwise.
Example:
fclose :handle
parse
w
—> l
Parses a list expression represented as a word, outputs the
same expression represented as list. Useful for parsing data
read from files.
Example:
parse "abc —> [abc]
fopen "hello.txt "w 16 —> [1 16]
fwrite 1 word [1 2 3 4 55 6 7 8] "
fclose 1
fopen "hello.txt "r 0 —> [1 16]
make "x fread 1 16
fclose 1
:x —> 1 2 3 4 55 6 7 8 (as a single word)
make "y parse :x
:y —> [1 2 3 4 55 6 7 8] (as a list)
NXT display and buttons
print
v
—> -
Prints the value on NXT's LCD. The display is arranged into 16 columns and 8 rows.
Example:
print [Hello NXTLogo!]
draw
n l
—> -
Draw on LCD. The first argument specifies the drawing command, the second argument specifies its
arguments. Draw does not return a value.
command
1
2
3
4
5
|
drawing request
draw point
draw line
draw circle
draw rectangle
draw graphic
|
argument
[x y]
[x1 y1 x2 y2]
[x y radius]
[x y width height]
[x y filename]
|
Example:
draw 5 [10 10 faceopen.ric]
cs
v
—> -
Clears the NXT display. Does not return a value.
Example:
cs
gotoxy
n1 n2
—> -
Moves the LCD printing cursor to the specified location. Rows are indexed from the bottom (0) to the top (7),
and columns are indexed from the left (0) to the right (15). The next print command will print
the first character at the specified location.
Example:
gotoxy 0 7
button
n
—> n
Determine the state of a button. The first argument specifies the button:
1 - right arrow
2 - left arrow
3 - orange square button
The returned value is 0, if the button was not pressed since the last call of this
function for the same button. Otherwise, it returns 2 * N + P, where N
is the number of times the button was pressed and released, and P is the current state
of the button (0-released, 1-pressed).
Example:
button 2 —> 2
sound
w1 n
—> -
Start playing specified file from the NXT's flash memory at a specified volume.
The volume is in the interval 0-4.
Example:
sound "Woops.rso 3 wait 1000
tone
n1 n2 n3
—> -
Start playing tone of specified frequency, duration and volume.
Example:
tone 440 100 3 wait 150
Motor and sensor control
motor
n1 w2 n3
—> -
Controls motor. The first argument specifies the motor(s) to operate on, the second argument
specifies the command, and the third argument specifies the argument (it must be supplied even if the
argument is not used). In particular:
motor specification
0
1
2
3
4
5
6
|
meaning
OUT_A
OUT_B
OUT_C
OUT_AB
OUT_AC
OUT_BC
OUT_ABC
|
motor command
onfwd
onrev
float
off
|
argument
power
power
(ignored)
(ignored)
|
meaning
start moving this way
start moving that way
stop moving, but do not use active braking
active braking (should be applied only for short time)
|
motor does not return any value.
Note: regulation mode coming soon.
Example:
motor 4 "onfwd 50
readmotor
w1 w2
—> n
Reads the tacho counters from motor, first argument is motor number 0, 1, or 2,
and the second argument is
0 - regulation mode
1 - tacho limit
2 - rotation count
Example:
make "tl readmotor 0 1
sensor
n
—> n
Read value of specified sensor 1-4. If using the ultrasonic sensor, add 4 to the sensor number
Example:
print sensor 1
print sensor 6
setsensor
n1 n2 n3
—> -
Setup sensor type. Before using sensor, it should be configured for the correct sensor type.
The first argument is the sensor number: 1-4. The second argument is the sensor type, and the
third argument is the sensor mode.
Sensor types
0 - raw
1 - switch
2 - temperature
3 - refl
4 - angle
5 - light active
6 - light inactive
7 - sound db
8 - sound dba
9 - custom
10 - lowspeed (ultrasonic)
11 - lowspeed with 9V
12 - high speed port
|
Sensor modes
32 - boolean output (0/1)
64 - transition count
96 - period counter
128 - percentage (0-1023 value normalized to 0-100)
160 - temperature in C
192 - temperature in F
224 - angle steps
31 - slope mask
224 - mode mask
|
Example:
setsensor 4 10 0 - setup ultrasonic sensor on port 4
setsensor 1 5 128 - setup active light sensor on port 1
battery
—> n
Returns the state of the NXT battery in cV.
Example:
battery —> 775
Code examples
Drawing a chart from distance measurements
Description: This example shows how to measure data using ultrasonic distance
sensor and how to quickly visualize the result in Imagine.
Connect ultrasonic sensor to port 4, place the robot with installed sensor facing forward
in front of a wall.
Define the function measure (either load it from a text file in NXT flash,
or by evaluating the to command). Finally, evaluate the measure function call
from Imagine to obtain a list of 100 values, which you can visualize by clicking the
"Plot" button. Between the low and high beep, move the robot with the sensor forward and
backward to create various distances.
to "measure []
[
tone 440 100 2
setsensor 4 10 0
let "m []
repeat 100
[
make "m fput sensor 8 :m
wait 50
]
tone 880 100 2
op :m
]
Line following robot
Description: This is a simple line-following procedure for a robot with one light
sensor connected to sensor 3 installed in the front of the robot facing down.
The left and right motors are connected to motor ports A and C, respectively.
The program runs forever and has to be stoped by the "Stop Logo" button in
the Imagine NXT project, or by the dark gray button on the NXT brick.
The robot follows the gradient between the black line and white paper. Adjust
the performance by making the line wide enough and set the speed appropriately
(here using speed 40). You may need to exchange the onrev and onfwd depending on the way your motors are installed.
to "follow []
[
setsensor 3 5 128
motor 0 "onrev 40
while ["true]
[
while [ge? sensor 3 59] []
motor 0 "float 0
motor 2 "onrev 40
wait 50
while [lt? sensor 3 60] []
motor 2 "float 0
motor 0 "onrev 40
]
]
To start the program, call the function follow.
Computing Fibonacci numbers
The following program computes Fibonacci numbers defined by recurrent rule
f(1) = 1
f(2) = 1
f(N+2) = f(N) + f(N + 1)
The program utilizes tail recursion, which is currently not yet optimized to iteration in NXT Logo (coming soon),
but works anyway. We could use the while loop instead. Note that the valid range for the numbers
is currently only -8191..8191 (signed 14-bit). Also note the dynamic scoping
of Logo language - the variable n is visible in the function fib2, i.e. it does not have to be passed inside of an extra argument.
to "fib [:n]
[
op fib2 1 1
]
to "fib2 [:a :b]
[
if le? :n 1 [op :b]
make "n sub :n 1
op fib2 add :a :b :a
]
Example: fib 10 —> 55
Reversing a list
The following program reverses the order of elements in a list.
to "rev [:l]
[
rev2 :l []
]
to "rev2 [:l :a]
[
if empty? :l [op :a]
op rev2 bf :l fput first :l :a
]
Example: rev [1 2 3 4] —> [4 3 2 1].
NXT Logo utilities
Several utilities are coming together with NXT Logo, some of them can be used also independently. All utilities currently
assume communication over BlueTooth virtual serial port, and do not support sommunication over USB cable yet.
- setup.exe - simple installation program that does all the required steps of NXT Logo installation, after the zip-archive is uncompressed to a new directory. It can be started from command prompt, or by double-clicking the setup.exe from Windows Explorer window.
- nxtls.exe - command-line utility for displaying the list of files stored in the NXT flash
- nxtrm.exe - command-line utility for removing file(s) from NXT flash
- nxtcp.exe - command-line utility for uploading and downloading files and programs from/to NXT flash
- remote.exe - interactive communication with NXT Logo interpreter: text-based front-end that is alternative to Imagine Logo
- makeres.exe - used by developers to generate the NXT Logo data and source files from the specification in file reserved.txt.
- catcore.exe - utility that dumps the contents of file core.dat that contains memory dump of NXT Logo interpreter. The file core.dat is produced when run-time error occurs, or can be produced on request by the system command.
Usage of utilities:
setup - no arguments
---
nxtls virtual_serial_port
---
nxtrm nxt_port:filename (wildcards *.*, *.exe, filename.* are legal)
---
nxtcp [nxt_port:|src_path\]filename1 [[nxt_port:|dst_path\][filename2]]]
examples: nxtcp COM21:data.txt
- copy file data.txt from NXT to cuurent dir at PC
nxtcp fib.lgo COM21:
- copy file fib.lgo from PC to NXT
nxtcp COM21:data.txt measure.txt
- copy file data.txt from NXT to file measure.txt at PC
nxtcp prog.txt COM21:prog.lgo
- copy file prog.txt from PC to NXT file prog.lgo
nxtcp COM21:*.* backup_dir\
- copy all files from NXT to backup_dir on PC
nxtcp nxt_melodies\*.* COM21:
- copy all files from specified location to NXT
---
remote virtual_serial_port
---
makeres - no arguments
---
catcore - no arguments
History of changes
CVS contains the most recent version. After every important bugfix or feature introduction, there is a full release downloadable as zip-file. Here is the list of the downloadable versions and the changes as they were introduced:
* 2007/10/02:
- Hungarologo preview version - as presented at HungaroLogo conference, unstable;
* 2007/10/08:
- added nxt file system procedures: fopen, fread, fwrite, fclose
- operation for parsing word into list: parse
- corrected silly error regarding variables that was introduced with recent redesign of symbol tables
* 2007/10/13:
- minor change in newserial.ocx for communication from Imagine Logo
- fixed bl [1] and small issue with inefficient use of memory in fput
- fixed problem of spurious packets due to keepalive in nxt.imp through synchronization
* 2007/10/24
- added win32 console utility for communication with NXT Logo from Windows without the need of Imagine Logo
* 2007/11/05
- added NXT flash filesystem utilities nxtls, nxtrm, nxtcp, and the setup utility.
* 2007/12/05
- added nxt_motor and corrected some issues in nxt.imt
* 2007/12/14
- ported to Linux (with limited support for UCBLogo)
* 2009/07/24
- replaced the OCX component with executable program (does not require administator priviledges)