The workspace contains all the elements defined by the user. These are:
- The procedures
- The variables
- The property lists
Procedures are a kind of “program”. When a procedure is called, the instructions in the body of
the procedure are executed. A procedure is defined with the keyword to.
to name_of_procedure :v1 :v2 :v3 .... [:v4 ....] [:v5 ....]
Body of the procedure
end
|
- name_of_procedure is the name given to the procedure.
- :v1 :v2 :v3 stand for the variables used internally in this procedure. (local variables).
- [:v4 ... ], [:v5 ...] are optional variables that we could add into the procedure (go below for more
explanations).
- Body of the procedure represents the commands to be executed when this procedure is called.
Eg:
to square :s
repeat 4[fd :s rt 90]
end
|
The procedure is called square and takes a parameter called s. square 100 will therefore produce a square, the
length of whose sides is 100. (See the examples of procedures at the end of this manual.)
Since version 0.7c, it is possible to insert comments in the code preceded by #.
to square :s
#this procedure allows a square to be drawn whose side equals :s.
repeat 4[fd :s rt 90] # handy, isn’t it?
end
It’s now possible to add optional arguments to XLogo’s procedure. Look at the example
below:
to poly :n [:l 10]
repeat :n [fd :l rt 360/:n]
end
# this command will draw a regular polygon with
# 20 sides of 10
poly 20
|
During the interpretation, the variable :l has been replaced by its default value, I mean 10. If I we want to
modify this value, we have to call the procedure poly between parenthesis to notice to the interpreter that we’re
going to use optional arguments.
# This command will draw a regular polygon with 20
# sides. Every side is 5 long.
(poly 20 5)
# This is a square with each side equal to 100
(poly 4 100)
|
It is possible to follow the working of a program or have it show the procedures which are
working. This mode can also show if the procedures provide arguments thanks to the primitive output.
Activate trace mode
stoptrace will disactivate the trace mode.
A small example with the factorial (see page 84).
trace pr fac 4
fac 4
fac 3
fac 2
fac 1
fac returns 1
fac returns 2
fac returns 6
fac returns 24
24
|
There are two kinds of variables:
- Global variables: these are always accessible from any location in the program.
- Local variables: these are only accessible in the procedure where they are defined.
In this version of LOGO, local variables are not accessible in sub-procedures. At the end of the procedure, the local
variables are deleted.
If the local variable word1 exists, assigns it the value arg2. If not, creates a global variable word1 and assigns it the
value arg2.
Eg: make “a 100 assigns the value 100 to the variable a
Creates a variable called arg1. If arg1 is a list, creates all variables contained in the list. Note, this is not initialised.
To assign it a value, see make.
Creates a new local variable and assigns it the value arg2.
Define a new procedure called word1.
list1 contains several lists:
- The first list contains all variables and optional variables.
- Then, each following list represents a procedure line.
def "polygon [ [nb length] [repeat :nb[fd :length rt 360/:nb] ]
|
—-> this command defines a procedure called polygon with two variables (:nb and :length). This procedure
draws a regular polygon, we can choose the number of sides and their length.
Returns all information about the procedure called word1. It gives a list which contains several lists.
- The first list contains all variables and optional variables about the procedure word1.
- Then, each following list represents a procedure line.
This primitive is of course associated to define.
returns the value of the variable word1. thing "a is similar to :a
Deletes the procedure calling arg1 or all procedures that contains the list arg1.
Deletes the variable arg1 or all variables that contains the list arg1.
erasepropertylist, erpl arg1 |
Deletes the property list arg1 or all property lists that contains the list arg1.
Deletes all the variables, property lists and procedures currently running.
Quit XLOGO.
Returns a list which contains all the procedures currently defined.
Returns a list which contains all the defined variables.
Returns a list which contains all the property lists currently defined.
Enumerates all primitives for the current language.
Returns a list which contains 3 lists. The first list contains all procedures names, the second all variables names and
the last all propert lists names.
Executes the list of instructions contained in list1
Run a system command from XLOGO. list1 must contain several lists which represent any word constituing the
command. Some few examples:
externalcommand [[gedit][/home/xlogo/file.txt]]
Run the application gedit and loads the file /home/xlogo/file.txt (Linux)
externalcommand [[notepad][C: /file.txt]]
Run the application notepad and loads the file called C: /file.txt (Windows)
This syntax a bit ... particular allow to specify white spaces in file path.
Now, you can define property lists with XLogo. Each list has a specific name and contains
some key-value couples.
For example, we can consider a property list named ”car“. It should contain the key ”color“ associated to the value
”red“, or the key ”type“ and the value ”4x4“.
To handle these lists, we can use the following primitives:
pprop putproperty listname key value |
Adds a property to the property list named listname. value will be accessible with the key key. If no property list
named listname exists, then it will be created.
gprop getproperty listname key |
Returns the value associated with the key key in the property list called listname. If this property list doesn’t exist
or if there isn’t any valid key, it returns an empty list.
rprop removeproperty listname key |
Removes the corresponding couple key-value in the property list listname.
plist propertylist listname |
Displays all key-value couples contained in the property list called listname.
Let’s return to the property list called ”car“.
# Filling property list
pprop "car "color "red
pprop "car "type "4x4
pprop "car "vendor "Citroën
# Display one value
print gprop "car "color
red
# Display all elements
print plist "car
color red type 4x4 vendor Citroën
|
By default, lists the contents of the directory. (Equivalent to the ls command for Linux users and the dir command
for DOS users)
Load the image file contained in the list. Its upper left corner will be placed at the turtle’s location. The only
supported formats are .png and .jpg. The path specified must be relative to the current folder. Eg: setdir
"C:\\my_images_dir loadimage "turtle.jpg
Saves the drawing area image in file specified with word in the current directory.
The supported format are png and jpg. If you don’t specify any extension, the default format is png.
The list contains four numbers [XminY minXmaxY max] which allow to specify the two corners if you want a zone
selection. An empty list is equivalent to the whole image. An example:
## In the editor
to test
repeat 20 [
forward 30 right 18
# Save all images as 1.png, 2.png ... 20.png
saveimage word repcount ".png [-50 150 200 -150]
]
end
## In the command line:
test
clearscreen hideturtle repeat 20 [loadimage word repcount ".png]
|
And you create a little animation!
setdirectory, setdir word1 |
Specifies the current directory. The path must be absolute. The directory must be specified with a
word.
changedirectory, cd word1 |
Allows to choose the current directory. The path is related to the current directory. You can use the ’..’ notation to
refer to the parent directory.
Gives the current directory. The default is the user’s home directory, ie /home/your_login for Linux users,
C:∖WINDOWS for Windows users.
A good example to explain this: save "test.lgo [proc1 proc2 proc3] saves in the file test.lgo in the current
directory the procedues proc1, proc2 et proc3. If the extension .lgo is omitted, it is added by default. word1 gives
a relative path starting from the current directory. This command will not work with an absolute
path.
saved "test.lgo saves in the file test.lgo in the current directory the collection of procedures currently defined. If
the extension .lgo is omitted, it is added by default. word1 gives a relative path starting from the current directory.
This command will not work with an absolute path.
Open the editor with all the procedures specified in the list arg1 or in the word arg1.
Open the editor with all the currently defined procedures.
Opens and reads the file word1. For example, to delete all the defined procedures and load the file test.lgo, you
would use: efns load "test.lgo. word1 gives a relative path starting from the current directory. This command
will not work with an absolute path.
When you want to read or write in a file, you must first open a flow toward this file. The argument file must be the
name of the file you want. You must use a phrase to show the name of the file in the current directory. The id
argument is the number given to this flux so as to identify it.
Shows the list of the various open fluxes with their identifiers.
Opens the flow which identifier corresponds to the number used as argument and then reads a line in this file.
Opens the flux which identifier corresponds to the number used as argument and then reads a character
in this file. This primitive sends back a number representing the value of the character (similar to
readchar).
Writes the text line included in list at the beginning of the file identified thanks to the identifier id. Be careful, the
writing is effective only after the flow has been closed by the primitive closeflow.
Writes the text line included in list at the end of the file identified thanks to the identifier id. Be careful, the writing
is effective only after the flux has been closed by the primitive closeflow.
Closes the flux when its identifier number is written as argument.
Sends back "true if it is the end of the file. Otherwise sends back "false.
Here is an example of the use of primitives allowing to read and write in a file. I will give this example in a
Windows-type framework. Other users should be able to adapt the following example.
The aim of this file is to create the file c:\example containing the following three lines:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Abcdefghijklmnopqrstuvwxyz
0123456789
# You open a flow towards the desired file. This flow is given the number 2
setdirectory "c:\\
openflow 2 "example
# You type the desired lines
writelineflow 2 [abcdefghijklmnopqrstuvwxyz]
writelineflow 2 [abcdefghijklmnopqrstuvwxyz]
writelineflow 2 [0123456789]
# You close the flux to end the writing
closeflow 2
|
Now, you can see the writing procedure went alright:
# You open a flow towards the file you want to read. This flow is given the number 0
openflow 0 "c:\\example
# You read the one after the other the different lines from the file
pr readlineflow 0
pr readlineflow 0
pr readlineflow 0
# You close the flow
closeflow 0
|
if you wish to add the line ’Great !’:
setdirectory "c:\\
openflow 1 "example]
appendlineflow 1 [Great!]
closeflow 1
|