Gentle Knowledge Code Editor

Text Output:

Cursor Commands

reset

Clears the screen, moves the cursor home, sets the penwidth to 1 pixel, sets the pen down on the canvas, and changes its color to black.

clearscreen, clear, cs

Clears the screen, moves the cursor home.

clean

Clears the screen but keeps the cursor in its current position.

home

Moves the cursor to the home position. The home position is located in the center of the canvas at (250, 250). The origin (0,0) of the canvas (where the cursor moves) is in the top left corner of the canvas. X-values increase from left to right and y-values increase from top to bottom. The dimensions of the canvas are 500 pixels by 500 pixels.

forward n, fw n

Moves the cursor n pixels forward (in the direction that it is currently facing) from its current position.

backward n, bw n

Moves the cursor n pixels backward (in the opposite direction that it is currently facing) from its current position.

right d, rt d

Turns the cursor d degrees clockwise from the direction it is currently facing.

left d, lt d

Turns the cursor d degrees counterclockwise from the direction it is currently facing.

stop

Stops the cursor. Stop also stops the current function or repeat block (see below).

setx x

Sets the x-coordinate of the cursor to x.

sety y

Sets the y-coordinate of the cursor to y.

setxy x y

Sets the x-coordinate of the cursor to x and the y-coordinate to y.

setheading d

Turns the cursor d degrees clockwise from the y-axis of the screen.

getx

Returns the x-coordinate of the cursor.

gety

Returns the y-coordinate of the cursor.

getheading

Returns the heading of the cursor. When the cursor is facing the top of the screen its heading is 0 or 360 degrees, and when it is facing the right side of the screen its heading is 90 degrees.

penup, pu

Makes the cursor stop drawing.

pendown, pd

Makes the cursor resume drawing.

color [r g b]

Sets the color that the cursor will draw with to the given rgb value (which range from 0 to 255). color [0 0 0] is black and color [255 255 255] is white.

penwidth w

Sets the width of the line that the cursor draws to w pixels.

circle r

Draws a circle of radius r around the cursor.

arc r d

Draws a d degree arc of radius r around the cursor. The arc is drawn clockwise starting from the direction in which the cursor is facing.

Text Output

print x

Prints (on the screen) the value of x which can be a string, a number, or a list.

cleartext

Clears any text that has been printed on the screen.

Language Control Structures

repeat n [ ... ]

The commands inside the square brackets will be repeated n times.

if condition [ ... ]

If the condition is true the commands inside the square brackets will be executed. For example,
if 5 < 10 [fw 20] moves the cursor 20 pixels forward because the condition is true.

; comments

Anything following a semicolon will not be executed and can be used to annotate the code.

Variables

A variable can be a number, a string, a boolean (true or false), or a data structure (a list).

Numbers

Numbers can be whole (eg. 12) or can be written as decimals (eg. 5.8).

Strings

A string is a sequence of characters preceded by double quotation marks. For example: "turtle

Setting variables

Every variable has a name which is a string. To create a new variable and give it a value or to change the value of an existing variable use the "make" command:


;this creates a variable called x
;and sets its value to 5:

make "x 5

;this changes the value of x
;to the string "hello":

make "x "hello 

;this creates a variable called lst
;and makes its value the list [1 2 3]:

make "lst [1 2 3] 

Getting variable values

Every variable has a value which can be accessed by prepending a ":" to the name of the variable:


;this creates a variable called y
;and sets its value to 10:

make "y 10

;this will print 10 on the screen:
print :y

;this increases the value of y by 2:

make "y :y + 2

Functions

A sequence of commands that is frequently used can be turned into a function that can be used instead of repeating the commands many times. The syntax is:


to function_name input1 input2 ...
	...
end

A function always starts with the "to" keyword, followed by the name of the function (no spaces are allowed in the name), and a list of inputs to the function. Below that comes the actual body of the function and the function must end with the "end" keyword. For example the following function draws a square:


to square :sidelength
  repeat 4 [
    fw :sidelength
    rt 90
  ]
end

The above code by itself will not draw a square; it is simply a definition of a function. To draw a square the function needs to be called (used with actual inputs). For example, the following code will draw a square with side length 100 pixels:


square 100

output v

A function can not only group commands, it can also produce output. The value after the output keyword is the value that the function returns (outputs). For example, the following code adds two numbers and returns their value:


to add :a :b
  output :a + :b
end

print add 4 5 ;prints 9 on the screen

Conditional Expressions

=

a = b returns true if a is equal to b, and false otherwise. Numbers as well as strings can also be compared this way. For example, "hello = "hello returns true and "hello = "apple returns false.

!=

a != b returns true if a is not equal to b, and false otherwise. Numbers as well as strings can be compared this way. For example, "hello != "hello returns false and "hello != "apple returns true.

<

a < b returns true if a is less than b, and false otherwise.

<=

a <= b returns true if a is less than or equal to b, and false otherwise.

>

a > b returns true if a is greater than b, and false otherwise.

>=

a >= b returns true if a is greater than or equal to b, and false otherwise.

Logic

true, false

true and false are built-in values boolean values.

not a

Returns true if a is false and returns false if a is true.

and a b

Returns true if both a and b are true and returns false otherwise.

or a b

Returns false if both a and b are false and returns true otherwise.

Lists

Sometimes a program needs to store and access a large or growing amount of data and for this lists are useful. A list is a sequence of values (called items or elements) that can contain numbers, strings, or other lists. A list is created by putting square brackets around a set of values separated by spaces. For example, ["a "b "c] is a simple list that contains three elements that are strings. On the other hand,
[1 2 "a 2 ["a "b]] is a more complicated list whose last element is the 2 element list ["a "b].

first lst, head lst

The functions first or head take a list (lst) as input and return the first element of the list.

butfirst lst, tail lst

The functions butfirst or tail take a list (lst) as input and return a list that contains everything except the first element of the list.

last lst

The function last takes a list (lst) as input and returns the last element of that list.

butlast lst

The function butlast takes a list (lst) as input and returns a list that contains everything except the last element of that list.

size lst

The function size takes a list (lst) as input and returns the number of elements in that list.

empty? lst

The function empty? takes a list (lst) as input and returns true if the list has no elements and false otherwise.

fput val lst

The function fput takes a list (lst) and another value (val) as input and returns the same list but with val added to the beginning of the list.

lput val lst

The function lput takes a list (lst) and another value (val) as input and returns the same list but with val added to the end of the list.

item index lst

The function item takes a list (lst) and a positive number (index) as input and returns the value of the element whose place in the list is given by index. The first element of the list has index equal to 0, the second element has index equal to 1, etc. For example, item 2 ["a "b "c "d] returns the string "c".

setitem index lst val

The function setitem takes a list (lst), a positive number (index), and another value (val) as input and returns the same list but with the value of the element whose place in the list is given by index set to val. The first element of the list has index equal to 0, the second element has index equal to 1, etc. For example, the following code
setitem 2 ["a "b "c "d] 17
returns the list ["a "b 17 "d].

Math

+, (sum a b c ...)

Two numbers can be added using the plus operator like this: 1 + 2. Multiple numbers can be added using the 'sum' command like this: (sum 1 2 3 4).

-, difference a b

A number can be subtracted from another number using the minus operator like this: 6 - 3 or using the 'difference' command like this: difference 6 3.

*, (product a b c ...)

Two numbers can be multiplied using the multiplication operator like this: 2 * 3. Multiple numbers can be multiplied using the 'product' command like this: (product 1 2 3 4 5).

/, divide a b

A number can be divided by another number using the divide operator like this: 6 / 3 or using the 'divide' command like this: divide 6 3.

%, mod a b

a % b and mod a b both return the remainder when a is divided by b.

sqrt n

Returns the square root of n.

power m n

Returns m raised to the nth power.

exp n

Returns e (Euler's number which is approximately equal to 2.71828) raised to the nth power.

ln n

Returns the natural logarithm of n.

log10 n

Returns the base 10 logarithm of n.

pi

Returns the value of π (approximately 3.141592653589793).

sin d

Returns the sin of d where d is in degrees.

cos d

Returns the cos of d where d is in degrees.

tan d

Returns the tan of d where d is in degrees.

radsin r

Returns the sin of r where r is in radians.

radcos r

Returns the cos of r where r is in radians.

radtan r

Returns the tan of r where r is in radians.

arcsin x

Returns the arcsin of x in degrees.

arccos x

Returns the arccos of x in degrees.

arctan x

Returns the arctan of x in degrees.

radarcsin x

Returns the arcsin of x in radians.

radarccos x

Returns the arccos of x in radians.

radarctan x

Returns the arctan of x in radians.

random n, rand n

Random n (or rand n) returns a random number in the interval [0, n). For example, random 4 would return 0, 1, 2, or 3.

int n

Returns n rounded to the greatest integer that is less than n.

round n

Returns n rounded to the nearest integer.

Your browser doesn't support canvas tags. Please upgrade to a recent version of Firefox or Opera.