|
FunctionsLibreLogo has some predefined functions that can be useful in different contexts. Here you find an overview of these. 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: Returns the integer part of a decimal number. :variable = INT 20,14 ; :variable = 20 Changes numbers written in a string to decimal numbers :variable = 3 * FLOAT "10,5" ; :variable = 31,5 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.) Returns the square root of a number or an expression. PRINT SQRT 100 ; Returns 10 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 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 Rounds a decimal numbers to the nearest integer. PRINT ROUND 5,5 ; Returns 6 PRINT ROUND -5,5 ; Returns -6 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 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. 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. FOR :x IN RANGE 20 50 10 [ PRINT :x ] ; Result: 20 30 40
FOR :i IN RANGE 10 100 5 [ 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
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'] (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' The function can also use RegEx (regular expression) for those who master it. 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 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. Converts a list to a Python set. See help for LibreLogo. Not explored. (A tuple is usually a common list which can not be changed, a sort of constant.) Not explored. :string = 'Text and more text' I do not know what this means. |
© Context and design: Kolbjørn Stuestøl | Stuestøl homesite (in Norwegian) | Modified February 11 2016 |