Turtle

Guide › The REPL

The REPL

Run turtle with no file to get Turtle's interactive prompt: type a line, press Enter, see the result. The code is colored as you type.

$ turtle
Turtle v1.0.0 — type help for help, quit to leave
>>> nums = list [3, 1, 2]
>>> nums at length * 2
6
>>> def double[x]
...     return x * 2
... def [end]
>>> double[21]
42
>>> 1 div 0
error (math): division by zero
>>> quit

Colors

Colors come from Turtle's own lexer, so they always match the language. They're grouped by what a word does, in the same twelve schemes as the VS Code extension, color for color:

Group What
keywordsdef, if, loop, at, div ..., and a library's words once imported (random, check, log)
importsan import line, all of it
functionscalls name[...], library functions, methods after at; bold where defined, bold italic for a test_ function
datalist, set, map, matrix, assemble and your types (bold where made)
showshow and warn
text"text"
numbersnumbers, true, false, none
theoriestheory, its sections, and its word where it's used

Comments are italic gray in every scheme, and a value the REPL shows takes the functions color.

Picking a scheme

Type colors for the picker: the schemes down the left, and a short program on the right in the colors of the one the cursor is on.

>>> colors
  shade: ← auto (dark) →

  turtle (now)        │  import time [today]
  classic             │  assemble Order [item, qty]
> ocean               │
  sunset              │  // the items, and how many
  forest              │  def total[orders]
  soft                │      sum = 0
  dusk                │      [loop][o in orders]
  okabe-ito           │          sum = sum + o at qty
  blue-orange         │      [loop][end]
  teal-rose           │      show "total: " . sum
  high-contrast       │      return tally orders
  no-color            │  def [end]

  ↑↓ scheme   ←→ shade   Enter keeps it   Esc leaves it
Keys Do
Up / Downthe scheme; the program recolors as you go
Left / Rightthe shade: auto, dark, light
Enterkeep it (and for next time)
Esc, q or Ctrl-Cleave the colors as they were

Or name it, with no picker:

>>> colors okabe-ito
colors: okabe-ito, dark (auto)
>>> colors classic light
colors: classic, light
>>> colors auto
colors: classic, dark (auto)

Without keys to press (input piped in, or a window narrower than the picker's 55 columns) colors lists the schemes instead, each in its own colors.

They work in macOS Terminal and iTerm, Linux terminals, and Windows Terminal / PowerShell on Windows 10 and later. Set the environment variable NO_COLOR (to anything) for no colors at all; colors still records a choice for when they're back.

Keys

Keys Do
Left / Right, Ctrl-B / Ctrl-Fmove
Ctrl-Left / Ctrl-Right, Alt-B / Alt-Fmove a word
Home / End, Ctrl-A / Ctrl-Estart / end of the line
Up / Down, Ctrl-P / Ctrl-Nearlier / later lines (history)
Backspace, Deletedelete a character
Ctrl-W, Alt-Backspacedelete the word before the cursor
Ctrl-K / Ctrl-Udelete to the end / start of the line
Tabindent (four spaces)
Ctrl-Lclear the screen
Ctrl-Ccancel the line, or stop running code
Ctrl-Dleave (on an empty line)

History is kept between sessions in ~/.turtle_history (the last 1,000 lines).

Commands

A line with just one of these words (and no variable or function of that name) is a command:

Command Does
helpthe commands and keys
help lineara library: what it's for, and each function in a line
help reshapeone function or method in full, with an example
help mwhat your variable m is, and its methods and libraries
help lista kind of value's methods (list, set, map, string, integer, float, matrix)
help ifa keyword; help keywords lists them all
quit (or exit, or Ctrl-D)leave
clearclear the screen
nameslist your variables and functions
load file.turtlerun a file into this session: its functions and values are then yours to use
save file.turtlewrite the entries you've run (the ones without errors) to a file
colorspick a color scheme: Up/Down the scheme, Left/Right the shade, Enter keeps it (Colors)
colors okabe-itouse a scheme; add light, dark or auto for the shade. Kept for next time

exit[code] from import system leaves too. The same help works in a program as help["linear"] or help[m]; see Finding things out.

Not a terminal

With input piped in, turtle runs it as a program: echo 'show 1 + 2 .' | turtle prints 3.

Example

A session can be typed in, or piped in. Each line runs as it is read; show prints its value. Here three lines are piped in, the first two define and print a value:

$ printf 'x = 5\nshow x * 2 .\nshow "hi" .\n' | turtle
10
hi
Top