Turtle LibreLogo
Tilbake til startsida
 

 

Functions

LibreLogo has some predefined functions that can be useful in different contexts. Here you find an overview of these.

RANDOM

Returns a random value from a collection of values. If the value should be a number, there must be an upper limit value. Use:

RANDOM 100 ; Random decimal numbers (16 digits) from and including zero through 100 (0 <= x <100).

RANDOM "abcdefghijklm" ; Random letter from the text

RANDOM [1, 2] ; Random list item 1 or item 2

Selecting a random list item does not work in my version (5.0) of LibreLogo. I had to use a workaround by defining the list separately and then use the random function:
:l LIST [3, 4, 13, 24]
RANDOM :l ; Return either 3, 4, 13 or 24
(You may omit the word LIST in the formula above. It is used here to specify that this is a list.)

INT

Returns the integer part of a decimal number.

:variable = INT 20,14 ; :variable = 20

FLOAT

Changes numbers written in a string to decimal numbers

:variable = 3 * FLOAT "10,5" ; :variable = 31,5

STR

Changes numbers to text string.

PRINT 'This is the number ' + STR (5)

This will print “This is the number 5”. (The number 5 can be replaced with a variable). If you omit STR in the expression, you get an error message. If you try to print out PRINT 4 + 9 you will get the sum of this numbers (13). If you want to print exactly as it stands, you must use PRINT STR (4) + '+' + STR (9). (In Help for LibreLogo the parenthesis are omitted in the examples, but this does not always work. Better to use them.)

SQRT

Returns the square root of a number or an expression.

PRINT SQRT 100 ; Returns 10

SIN

Returns the sin of a number or an expression.

PRINT SIN 90 ; Returns 0,8939966636005579

PRINT SIN 90 * PI/180 ; Returns 1,0 which is the sine of 90° in radians

COS

Returns the cosinus to a number or an expression.

PRINT COS 90 ; Returns -0,4480736161291701

PRINT COS 90 * PI/180 ; Returns 6,123233995736766e-17 which is the cosines of 90° i radians

ROUND

Rounds a decimal numbers to the nearest integer.

PRINT ROUND 5,5 ; Returns 6

PRINT ROUND -5,5 ; Returns -6

ABS

Returns the absolute value of a number. (Absolute value is the positive value of the number, even if the number is negative.)

PRINT ABS -15 ; Returns 15

COUNT

Returns the count of items in an expression.

PRINT COUNT "Some text" ; Returns 9, which is the number of characters (including spaces) in the string.

PRINT COUNT [1, 2, 3] ; Returns 3, which is the size of the list.

RANGE from to step

Defines a range for a list instead of defining each item separately. “from” is the initial value, “to” is the ending value and “step” is the length of each step, i.e. the interval between values. If only one number is stated, this is interpreted as the “to” value. The “from” value is set to 0 and the “step” to 1. If two numbers are stated, this is interpreted as “to” and “from”. If the “from” value is greater than the ending value, you get no error message, but the function does not return anything. All numbers must be integers.

Remeber that a list could be predefined, as all possible numbers or all possible integers, or defined by you as shown below.

linjestil

FOR :x IN RANGE 20 50 10 [ PRINT :x ] ; Result: 20 30 40

FOR :i IN RANGE 10 100 5 [
FORWARD :i
LEFT 90
]

Result: The figure at right.

If you intend to write the range using PRINT RANGE 0 10 2 the output will be “range (0, 10, 2)” and not the expected “[0, 2, 4, 6, 8]”. To print the range, you must difine it as a list (see below): PRINT LIST RANGE 0 10 2

FOR :x IN RANGE 20 50 [ PRINT :x ] ; Result: 20 21 22 23 … 49

SORTED

Returns a sorted list. The list can be a list of numbers or text, each of them being sorted in ascending order.

:list = list ['a', 'x', 's', 'b']
:sort = SORTED :list
PRINT :sort ; returns [u'a', u'b', u's', u'x']

(I have not figured out what the “u” in front of the characters means. The command PRINT :sort [0] returns “a” as expected.)

PRINT SORTED [5, 2, 9, 3]; returns [2, 3, 5, 9]

SUB (new, old, string)

“new", “old” and “string” are strings or single letters and should be written between quotation marks ("double" or 'single'). All instances of “old” will be replaced with "new” ones in the string “string”. A couple of examples:

:string = 'Text and more text'
:newstring = SUB ('t', 'T', :string)
PRINT :newstring ; Result: TexT and more TexT
PRINT SUB ('more', 'less', :string ; Result: Text and less text

The function can also use RegEx (regular expression) for those who master it.

LIST

This function is not actually necessary to use in ordinary programming. It creates an indexed list of numbers or text. The index counts from 0.

The pattern for a list looks like this: :array = LIST [element_1, element_2, element_n] where the elements are number or text. Text must be written between quotation marks. You get the same output if you write :array = [element_1, element_2, element_n], i.e. without the word LIST. Elements are fetched from the list by using the index: :n = :array [no]. I have not explored this more closely.

:angles = LIST [15, 30, 45, 60, 75, 90, 180] ; list with angles
REPEAT 40 [
  :x = INT RANDOM 7 ; Generates a random integer from 0 to 6
  BACK 50
  LEFT :angles[:x] ; Select angle number :x from the list.

MIN and MAX

Returns the smallest (MIN) or the largest (MAX) item in a list. The list can be numbers, letters or strings. In strings, the smallest or the biggest character will be returned, depending on the code value it has in the character code you are using.

SET

Converts a list to a Python set. See help for LibreLogo.

TUPLE

Not explored. (A tuple is usually a common list which can not be changed, a sort of constant.)

SEARCH

Not explored.

:string = 'Text and more text'
SEARCH ('e', :string)
return “<_sre.SRE_Match object at 0x11804838>”
I do not know what this means.

FINDALL

I do not know what this means.


© Context and design: Kolbjørn StuestølStuestøl homesite (in Norwegian)Modified February 11 2016