ludus/doc/prelude.md

1752 lines
50 KiB
Markdown
Raw Permalink Normal View History

2025-07-07 04:10:58 +00:00
# Ludus prelude documentation
These functions are available in every Ludus script.
The documentation for any function can be found within Ludus by passing the function to `doc!`,
e.g., running `doc! (add)` will send the documentation for `add` to the console.
For more information on the syntax & semantics of the Ludus language, see [language.md](./language.md).
2025-07-07 04:40:26 +00:00
The prelude itself is just a Ludus file, which you can see at [prelude.ld](../assets/prelude.ld).
2025-07-07 04:10:58 +00:00
## A few notes
**Naming conventions.** Functions whose name ends with a question mark, e.g., `eq?`, return booleans.
Functions whose name ends with an exclamation point, e.g., `make!`, change state in some way.
In other words, they _do things_ rather than _calculating values_.
Functions whose name includes a slash either convert from one value to another, e.g. `deg/rad`,
or they are variations on a function, e.g. `div/0` as a variation on `div`.
**How entries are formatted.** Each entry has a brief (sometimes too brief!) description of what it does.
It is followed by the patterns for each of its function clauses.
This should be enough to indicate order of arguments, types, and so on.
**Patterns often, but do not always, indicate types.** Typed patterns are written as `foo as :bar`,
where the type is indicated by the keyword.
Possible ludus types are: `:nil`, `:boolean`, `:number`, `:keyword` (atomic values);
`:string` (strings are their own beast); `:tuple` and `:list` (ordered collections), `:set`s, and `:dict`ionaries (the other collection types); `:pkg` (packages, which are quasi-collections); `:fn` (functions); and `:box`es.
**Conventional types.** Ludus has two types based on conventions.
* _Result tuples._ Results are a way of modeling the result of a calculation that might fail.
The two possible values are `(:ok, value)` and `(:err, msg)`.
`msg` is usually a string describing what went wrong.
To work with result tuples, see [`unwrap!`](#unwrap) and [`unwrap_or`](#unwrap_or).
That said, usually you work with these using pattern matching.
* _Vectors._ Vectors are 2-element tuples of x and y coordinates.
The origin is `(0, 0)`.
Many math functions take vectors as well as numbers, e.g., `add` and `mult`.
You will see vectors indicated in patterns by an `(x, y)` tuple.
You can see what this looks like in the last clause of `add`: `((x1, y1), (x2, y2))`.
## Functions by topic
### Bools
[and](#and)    [bool](#bool)    [bool?](#bool)    [false?](#false)    [not](#not)    [or](#or)    [true?](#true)
### Boxes
[box?](#box)    [store!](#store)    [unbox](#unbox)    [update!](#update)
### Dicts
[any?](#any)    [assoc](#assoc)    [coll?](#coll)    [count](#count)    [dict](#dict)    [dict?](#dict)    [diff](#diff)    [dissoc](#dissoc)    [empty?](#empty)    [get](#get)    [has?](#has)    [keys](#keys)    [random](#random)    [update](#update)    [values](#values)
### Environment and i/o
[console](#console)    [doc!](#doc)    [fetch\_inbox](#fetch\_inbox)    [fetch\_outbox](#fetch\_outbox)    [input](#input)    [key\_down?](#key\_down)    [keys\_down](#keys\_down)    [print!](#print)    [read\_input](#read\_input)    [report!](#report)
### Errors
[assert!](#assert)
### Lists
[any?](#any)    [append](#append)    [at](#at)    [butlast](#butlast)    [coll?](#coll)    [concat](#concat)    [count](#count)    [each!](#each)    [empty?](#empty)    [filter](#filter)    [first](#first)    [fold](#fold)    [index\_of](#index\_of)    [indexed?](#indexed)    [indices\_of](#indices\_of)    [join](#join)    [keep](#keep)    [last](#last)    [list](#list)    [list?](#list)    [map](#map)    [random](#random)    [range](#range)    [rest](#rest)    [second](#second)    [sentence](#sentence)    [slice](#slice)
### Llists
[car](#car)    [cdr](#cdr)    [cons](#cons)    [llist](#llist)
### Math
[abs](#abs)    [add](#add)    [angle](#angle)    [atan/2](#atan2)    [between?](#between)    [ceil](#ceil)    [cos](#cos)    [dec](#dec)    [deg/rad](#degrad)    [deg/turn](#degturn)    [dist](#dist)    [div](#div)    [div/0](#div0)    [div/safe](#divsafe)    [even?](#even)    [floor](#floor)    [gt?](#gt)    [gte?](#gte)    [heading/vector](#headingvector)    [inc](#inc)    [inv](#inv)    [inv/0](#inv0)    [inv/safe](#invsafe)    [lt?](#lt)    [lte?](#lte)    [max](#max)    [min](#min)    [mod](#mod)    [mod/0](#mod0)    [mod/safe](#modsafe)    [mult](#mult)    [neg](#neg)    [neg?](#neg)    [odd?](#odd)    [pi](#pi)    [pos?](#pos)    [pow](#pow)    [rad/deg](#raddeg)    [rad/turn](#radturn)    [random](#random)    [random\_int](#random\_int)    [range](#range)    [round](#round)    [sin](#sin)    [sqrt](#sqrt)    [sqrt/safe](#sqrtsafe)    [square](#square)    [sub](#sub)    [sum\_of_squares](#sum\_of_squares)    [tan](#tan)    [tau](#tau)    [to\_number](#to\_number)    [turn/deg](#turndeg)    [turn/rad](#turnrad)    [zero?](#zero)
### Processes
[alive?](#alive)    [await](#await)    [fledge](#fledge)    [flush](#flush)    [heed](#heed)    [hibernate!](#hibernate)    [monitor](#monitor)    [self](#self)    [send!](#send)    [sleep!](#sleep)    [spawn](#spawn)    [unlink!](#unlink)    [yield!](#yield)
### Results
[err](#err)    [err?](#err)    [ok](#ok)    [ok?](#ok)    [unwrap!](#unwrap)    [unwrap\_or](#unwrap\_or)
### Strings
[any?](#any)    [at](#at)    [chars](#chars)    [chars/safe](#charssafe)    [concat](#concat)    [count](#count)    [downcase](#downcase)    [empty?](#empty)    [join](#join)    [sentence](#sentence)    [show](#show)    [slice](#slice)    [slice\_n](#slice\_n)    [split](#split)    [string](#string)    [string?](#string)    [strip](#strip)    [to\_number](#to\_number)    [trim](#trim)    [upcase](#upcase)    [words](#words)
### Tuples
[any?](#any)    [at](#at)    [coll?](#coll)    [count](#count)    [empty?](#empty)    [first](#first)    [last](#last)    [ordered?](#ordered)    [rest](#rest)    [second](#second)    [tuple?](#tuple)
### Turtle graphics
[back!](#back)    [background!](#background)    [bk!](#bk)    [clear!](#clear)    [colors](#colors)    [fd!](#fd)    [forward!](#forward)    [goto!](#goto)    [heading](#heading)    [heading/vector](#headingvector)    [hideturtle!](#hideturtle)    [home!](#home)    [left!](#left)    [loadstate!](#loadstate)    [lt!](#lt)    [pc!](#pc)    [pd!](#pd)    [pencolor](#pencolor)    [pencolor!](#pencolor)    [pencolour](#pencolour)    [pencolour!](#pencolour)    [pendown!](#pendown)    [pendown?](#pendown)    [penup!](#penup)    [penwidth](#penwidth)    [penwidth!](#penwidth)    [position](#position)    [pu!](#pu)    [pw!](#pw)    [render\_turtle!](#render\_turtle)    [reset\_turtle!](#reset\_turtle)    [right!](#right)    [rt!](#rt)    [setheading!](#setheading)    [showturtle!](#showturtle)    [spawn\_turtle](#spawn\_turtle)    [turtle\_state](#turtle\_state)
### Types and values
[bool?](#bool)    [box?](#box)    [coll?](#coll)    [dict?](#dict)    [eq?](#eq)    [fn?](#fn)    [indexed?](#indexed)    [keyword?](#keyword)    [list?](#list)    [nil?](#nil)    [number?](#number)    [set?](#set)    [show](#show)    [some](#some)    [some?](#some)    [string?](#string)    [tuple?](#tuple)    [type](#type)
## All functions, alphabetically
[abs](#abs)    [add](#add)    [alive?](#alive)    [angle](#angle)    [any?](#any)    [append](#append)    [assert!](#assert)    [assoc](#assoc)    [at](#at)    [atan/2](#atan2)    [await](#await)    [back!](#back)    [background!](#background)    [between?](#between)    [bg!](#bg)    [bk!](#bk)    [bool](#bool)    [bool?](#bool)    [box?](#box)    [butlast](#butlast)    [car](#car)    [cdr](#cdr)    [ceil](#ceil)    [chars](#chars)    [clear!](#clear)    [coll?](#coll)    [colors](#colors)    [colours](#colours)    [concat](#concat)    [cons](#cons)    [console](#console)    [contains?](#contains)    [cos](#cos)    [count](#count)    [dec](#dec)    [deg/rad](#degrad)    [deg/turn](#degturn)    [dict](#dict)    [dict?](#dict)    [dissoc](#dissoc)    [dist](#dist)    [div](#div)    [div/0](#div0)    [div/safe](#divsafe)    [doc!](#doc)    [downcase](#downcase)    [each!](#each)    [empty?](#empty)    [eq?](#eq)    [err](#err)    [err?](#err)    [even?](#even)    [false?](#false)    [fd!](#fd)    [fetch](#fetch)    [fetch\_inbox](#fetch\_inbox)    [fetch\_outbox](#fetch\_outbox)    [filter](#filter)    [first](#first)    [floor](#floor)    [flush](#flush)    [fn?](#fn)    [fold](#fold)    [foldr](#foldr)    [forward!](#forward)    [get](#get)    [goto!](#goto)    [gt?](#gt)    [gte?](#gte)    [has?](#has)    [heading](#heading)    [heading/vector](#headingvector)    [heed](#heed)    [hibernate!](#hibernate)    [hideturtle!](#hideturtle)    [home!](#home)    [inc](#inc)    [index\_of](#index\_of)    [indexed?](#indexed)    [indices\_of](#indices\_of)    [input](#input)    [inv](#inv)    [inv/0](#inv0)    [inv/safe](#invsafe)    [join](#join)    [keep](#keep)    [key\_down?](#key\_down)    [keys](#keys)    [keys\_down](#keys\_down)    [keyword?](#keyword)    [last](#last)    [left!](#left)    [link!](#link)    [list](#list)    [list?](#list)    [llist](#llist)    [loadstate!](#loadstate)    [lt!](#lt)    [lt?](#lt)    [lte?](#lte)    [map](#map)    [mod](#mod)    [mod/0](#mod0)    [mod/safe](#modsafe)    [monitor!](#monitor)    [mult](#mult)    [neg](#neg)    [neg?](#neg)    [nil?](#nil)    [not](#not)    [odd?](#odd)    [ok](#ok)    [ok?](#ok)    [pc!](#pc)    [pd!](#pd)    [pencolor](#pencolor)    [pencolor!](#pencolor)    [pencolour!](#pencolour)    [pendown!](#pendown)  &nbsp
## Function documentation
### abs
Returns the absolute value of a number.
```
(0)
(n as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### add
Adds numbers or vectors.
```
()
(x as :number)
(x as :number, y as :number)
(x, y, ...zs)
((x1, y1), (x2, y2))
```
[Back to top.](#ludus-prelude-documentation)
---
### alive?
Tells if the passed keyword is the id for a live process.
```
(pid as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### angle
Calculates the angle between two vectors.
```
(v1, v2)
```
[Back to top.](#ludus-prelude-documentation)
---
### any?
Returns true if something is not empty, otherwise returns false (including for things that can't be logically full, like numbers).
```
([...])
(#{...})
((...))
(s as :string)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### append
Adds an element to a list.
```
()
(xs as :list)
(xs as :list, x)
```
[Back to top.](#ludus-prelude-documentation)
---
### assert!
Asserts a condition: returns the value if the value is truthy, panics if the value is falsy. Takes an optional message.
```
(value)
(msg, value)
```
[Back to top.](#ludus-prelude-documentation)
---
### assoc
Takes a dict, key, and value, and returns a new dict with the key set to value.
```
()
(d as :dict)
(d as :dict, k as :keyword, val)
(d as :dict, k as :string, val)
(d as :dict, (k as :keyword, val))
(d as :dict, (k as :string, val))
```
[Back to top.](#ludus-prelude-documentation)
---
### at
Returns the element at index n of a list or tuple, or the byte at index n of a string. Zero-indexed: the first element is at index 0. Returns nil if nothing is found in a list or tuple; returns an empty string if nothing is found in a string.
```
(i as :number)
(xs as :list, i as :number)
(xs as :tuple, i as :number)
(str as :string, i as :number)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### atan/2
Returns an angle from a slope. Takes an optional keyword argument to specify units. Takes either two numbers or a vector tuple.
```
(x as :number, y as :number)
(x, y, :turns)
(x, y, :radians)
(x, y, :degrees)
((x, y))
((x, y), units as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### await
Parks the current process until it receives an exit signal from the passed process. Returns the result of a successful execution or panics if the awaited process panics. If the other process is not alive, returns `nil`.
```
(pid as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### back!
Moves the turtle backward by a number of steps. Alias: `bk!`
```
(steps as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### background!
Sets the background color behind the turtle and path. Alias: `bg!`
```
(color as :keyword)
(gray as :number)
((r as :number, g as :number, b as :number))
((r as :number, g as :number, b as :number, a as :number))
```
[Back to top.](#ludus-prelude-documentation)
---
### between?
Returns true if a number is in the range [lower, higher): greater than or equal to the lower number, less than the higher.
```
(lower as :number, higher as :number, x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### bg!
Sets the background color behind the turtle and path. Alias: `bg!`
```
(color as :keyword)
(gray as :number)
((r as :number, g as :number, b as :number))
((r as :number, g as :number, b as :number, a as :number))
```
[Back to top.](#ludus-prelude-documentation)
---
### bk!
Moves the turtle backward by a number of steps. Alias: `bk!`
```
(steps as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### bool
Returns false if a value is nil or false, otherwise returns true.
```
(nil)
(false)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### bool?
Returns true if a value is of type :boolean.
```
(false)
(true)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### box?
Returns true if a value is a box.
```
(b as :box)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### butlast
Returns a list, omitting the last element.
```
(xs as :list)
```
[Back to top.](#ludus-prelude-documentation)
---
### car
Old-timey lisp `car`. Stands for 'contents of the address register.' Returns the first element in a `cons`ed pair (or any two-tuple).
```
((x, _))
```
[Back to top.](#ludus-prelude-documentation)
---
### cdr
Old-timey list `cdr`. Stands for 'contents of the decrement register.' Returns the second element in a `cons`ed pair, usually representing the rest of the list.
```
((_, x))
```
[Back to top.](#ludus-prelude-documentation)
---
### ceil
Truncates a number towards positive infinity. With negative numbers, it returns the integer part. With positive numbers, returns the next more-positive integer.
```
(n as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### chars
Takes a string and returns its characters as a list. Each member of the list corresponds to a utf-8 character.
```
(str as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### clear!
Clears the canvas and sends the turtle home.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### coll?
Returns true if a value is a collection: dict, list, tuple, or set.
```
(coll as :dict)
(coll as :list)
(coll as :tuple)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### colors
No documentation available.
---
### colours
No documentation available.
---
### concat
Combines lists, strings, or sets.
```
(x as :string)
(xs as :list)
(x as :string, y as :string)
(xs as :list, ys as :list)
(xs, ys, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### cons
Old-timey lisp `cons`. `Cons`tructs a tuple out of two arguments.
```
(x, y)
```
[Back to top.](#ludus-prelude-documentation)
---
### console
No documentation available.
---
### contains?
Returns true if a list contains a value.
```
(value, l as :list)
```
[Back to top.](#ludus-prelude-documentation)
---
### cos
Returns the cosine of an angle. Default angle measure is turns. An optional keyword argument specifies the units of the angle passed in.
```
(a as :number)
(a as :number, :turns)
(a as :number, :degrees)
(a as :number, :radians)
```
[Back to top.](#ludus-prelude-documentation)
---
### count
Returns the number of elements in a collection (including string).
```
(xs as :list)
(xs as :tuple)
(xs as :dict)
(xs as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### dec
Decrements a number.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### deg/rad
Converts an angle in degrees to an angle in radians.
```
(a as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### deg/turn
Converts an angle in degrees to an angle in turns.
```
(a as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### dict
Takes a list or tuple of `(key, value)` tuples and returns it as a dict. Returns dicts unharmed.
```
(d as :dict)
(l as :list)
(t as :tuple)
```
[Back to top.](#ludus-prelude-documentation)
---
### dict?
Returns true if a value is a dict.
```
(d as :dict)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### dissoc
Takes a dict and a key, and returns a new dict with the key and associated value omitted.
```
(d as :dict)
(d as :dict, k as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### dist
Returns the distance from the origin to a point described by x and y, or by the vector (x, y).
```
(x as :number, y as :number)
((x, y))
```
[Back to top.](#ludus-prelude-documentation)
---
### div
Divides numbers. Panics on division by zero.
```
(x as :number)
(_, 0)
(x as :number, y as :number)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### div/0
Divides numbers. Returns 0 on division by zero.
```
(x as :number)
(_, 0)
(x as :number, y as :number)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### div/safe
Divides a number. Returns a result tuple.
```
(x as :number)
(_, 0)
(x, y)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### doc!
Prints the documentation of a function to the console.
```
(f as :fn)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### downcase
Takes a string and returns it in all lowercase. Works only for ascii characters.
```
(str as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### each!
Takes a list and applies a function, presumably with side effects, to each element in the list. Returns nil.
```
(f! as :fn, [])
(f! as :fn, [x])
(f! as :fn, [x, ...xs])
```
[Back to top.](#ludus-prelude-documentation)
---
### empty?
Returns true if something is empty. Otherwise returns false (including for things that can't logically be empty, like numbers).
```
([])
(#{})
(())
("")
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### eq?
Returns true if all arguments have the same value.
```
(x)
(x, y)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### err
Takes a value and wraps it in an :err result tuple, presumably as an error message.
```
(msg)
```
[Back to top.](#ludus-prelude-documentation)
---
### err?
Takes a value and returns true if it is an :err result tuple.
```
((:err, _))
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### even?
Returns true if a value is an even number, otherwise returns false.
```
(x as :number)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### false?
Returns `true` if a value is `false`, otherwise returns `false`. Useful to distinguish between `false` and `nil`.
```
(false)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### fd!
Moves the turtle forward by a number of steps. Alias: `fd!`
```
(steps as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### fetch
Requests the contents of the URL passed in. Returns a result tuple of (:ok, <contents>) or (:err, <status code>).
```
(url)
```
[Back to top.](#ludus-prelude-documentation)
---
### fetch_inbox
No documentation available.
---
### fetch_outbox
No documentation available.
---
### filter
Takes a list and a predicate function, and returns a new list with only the items that produce truthy values when the function is called on them. E.g., `filter ([1, 2, 3, 4], odd?) &=> [1, 3]`.
```
(p? as :fn)
(p? as :fn, xs)
```
[Back to top.](#ludus-prelude-documentation)
---
### first
Retrieves the first element of an ordered collection: tuple, list, or string. If the collection is empty, returns nil.
```
([])
(())
("")
(xs as :list)
(xs as :tuple)
(str as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### floor
Truncates a number towards negative infinity. With positive numbers, it returns the integer part. With negative numbers, returns the next more-negative integer.
```
(n as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### flush
Clears the current process's mailbox and returns all the messages.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### fn?
Returns true if an argument is a function.
```
(f as :fn)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### fold
Folds a list.
```
(f as :fn, [])
(f as :fn, xs as :list)
(f as :fn, [], root)
(f as :fn, xs as :list, root)
```
[Back to top.](#ludus-prelude-documentation)
---
### foldr
Folds a list, right-associatively.
```
(f as :fn, [])
(f as :fn, xs as :list)
(f as :fn, [], root)
(f as :fn, xs as :list, root)
```
[Back to top.](#ludus-prelude-documentation)
---
### forward!
Moves the turtle forward by a number of steps. Alias: `fd!`
```
(steps as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### get
Takes a key, dict, and optional default value; returns the value at key. If the value is not found, returns nil or the default value.
```
(k as :keyword)
(d as :dict, k as :keyword)
(d as :dict, k as :keyword, default)
(k as :string)
(d as :dict, k as :string)
(d as :dict, k as :string, default)
```
[Back to top.](#ludus-prelude-documentation)
---
### goto!
Sends the turtle to (x, y) coordinates. If the pen is down, the turtle will draw a path to its new location.
```
(x as :number, y as :number)
((x, y))
```
[Back to top.](#ludus-prelude-documentation)
---
### gt?
Returns true if numbers are in decreasing order.
```
(x as :number)
(x as :number, y as :number)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### gte?
Returns true if numbers are in decreasing or flat order.
```
(x as :number)
(x as :number, y as :number)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### has?
Takes a key and a dict, and returns true if there is a non-`nil` value stored at the key.
```
(k as :keyword)
(d as :dict, k as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### heading
Returns the turtle's current heading.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### heading/vector
Takes a turtle heading, and returns a unit vector of that heading.
```
(heading)
```
[Back to top.](#ludus-prelude-documentation)
---
### heed
Parks the current process until it receives a reply, and returns whatever is replied. Causes a panic if it gets anything other than a `(:reply, result)` tuple.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### hibernate!
Ensures the current process will never return, allowing other processes to do their thing indefinitely. Does not unlink the process, so panics in linked processes will still bubble up.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### hideturtle!
If the turtle is visible, hides it. If the turtle is already hidden, does nothing.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### home!
Sends the turtle home: to the centre of the screen, pointing up. If the pen is down, the turtle will draw a path to home.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### inc
Increments a number.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### index_of
Takes a list or string returns the first index at which the scrutinee appears. Returns `nil` if the scrutinee does not appear in the search target.
```
(scrutinee as :list, target)
```
[Back to top.](#ludus-prelude-documentation)
---
### indexed?
Returns true if a value is indexed (can use `at`): list, tuple, or string.
```
(coll as :list)
(coll as :tuple)
(coll as :string)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### indices_of
Takes a list or string and returns a list of all the indices where the target appears. Returns an empty list if the target does not appear in the scrutinee.
```
(scrutinee as :list, target)
```
[Back to top.](#ludus-prelude-documentation)
---
### input
No documentation available.
---
### inv
Returns the inverse of a number: 1/n or `div (1, n)`. Panics on division by zero.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### inv/0
Returns the inverse of a number: 1/n or `div/0 (1, n)`. Returns 0 on division by zero.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### inv/safe
Returns the inverse of a number: 1/n or `div/safe (1, n)`. Returns a result tuple.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### join
Takes a list of strings, and joins them into a single string, interposing an optional separator.
```
([])
([str as :string])
(strs as :list)
([], separator as :string)
([str as :string], separator as :string)
([str, ...strs], separator as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### keep
Takes a list and returns a new list with any `nil` values omitted.
```
(xs)
```
[Back to top.](#ludus-prelude-documentation)
---
### key_down?
Returns true ie the key is currently pressed. Keys are indicated by strings. For non-alphanumeric keys, consult the documentation to get key codes.
```
(key as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### keys
Takes a dict and returns a list of keys in that dict.
```
(d as :dict)
```
[Back to top.](#ludus-prelude-documentation)
---
### keys_down
No documentation available.
---
### keyword?
Returns true if a value is a keyword, otherwise returns false.
```
(kw as :keyword)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### last
Returns the last element of an indexed value: list, tuple, or string.
```
(xs as :list)
(xs as :tuple)
(str as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### left!
Rotates the turtle left, measured in turns. Alias: `lt!`
```
(turns as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### link!
Links this process to another process. When either one dies--panics or returns--both are shut down.
```
(pid as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### list
Takes a value and returns it as a list. For atomic values, it simply wraps them in a list. For collections, conversions are as follows. A tuple->list conversion preservers order and length. Dicts return lists of `(key, value)`` tuples, but watch out: dicts are not ordered and may spit out their pairs in any order. If you wish to get a list of chars in a string, use `chars`.
```
(x)
```
[Back to top.](#ludus-prelude-documentation)
---
### list?
Returns true if the value is a list.
```
(l as :list)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### llist
Makes an old-timey linked list of its arguments, of LISt Processor fame.
```
(...xs)
```
[Back to top.](#ludus-prelude-documentation)
---
### loadstate!
Sets the turtle state to a previously saved state.
```
(state)
```
[Back to top.](#ludus-prelude-documentation)
---
### lt!
Rotates the turtle left, measured in turns. Alias: `lt!`
```
(turns as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### lt?
Returns true if numbers are in increasing order.
```
(x as :number)
(x as :number, y as :number)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### lte?
Returns true if numbers are in increasing or flat order.
```
(x as :number)
(x as :number, y as :number)
(x, y, ...zs)
```
[Back to top.](#ludus-prelude-documentation)
---
### map
Maps a function over a list: returns a new list with elements that are the result of applying the function to each element in the original list. E.g., `map ([1, 2, 3], inc) &=> [2, 3, 4]`. With one argument, returns a function that is a mapper over lists; with two, it executes the mapping function right away.
```
(f as :fn)
(kw as :keyword)
(f as :fn, xs)
(kw as :keyword, xs)
```
[Back to top.](#ludus-prelude-documentation)
---
### mod
Returns the modulus of x and y. Truncates towards negative infinity. Panics if y is 0.
```
(x as :number, 0)
(x as :number, y as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### mod/0
Returns the modulus of x and y. Truncates towards negative infinity. Returns 0 if y is 0.
```
(x as :number, 0)
(x as :number, y as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### mod/safe
Returns the modulus of x and y in a result tuple, or an error if y is 0. Truncates towards negative infinity.
```
(x as :number, 0)
(x as :number, y as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### monitor!
Subscribes this process to another process's exit signals. There are two possibilities: a panic or a return. Exit signals are in the form of `(:exit, pid, (:ok, value)/(:err, msg))`.
```
(pid as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### mult
Multiplies numbers or vectors.
```
()
(x as :number)
(x as :number, y as :number)
(x, y, ...zs)
(scalar as :number, (x, y))
((x, y), scalar as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### neg
Multiplies a number by -1, negating it.
```
(n as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### neg?
Returns true if a value is a negative number, otherwise returns false.
```
(x as :number)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### nil?
Returns true if a value is nil.
```
(nil)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### not
Returns false if a value is truthy, true if a value is falsy.
```
(nil)
(false)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### odd?
Returns true if a value is an odd number, otherwise returns false.
```
(x as :number)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### ok
Takes a value and wraps it in an :ok result tuple.
```
(value)
```
[Back to top.](#ludus-prelude-documentation)
---
### ok?
Takes a value and returns true if it is an :ok result tuple.
```
((:ok, _))
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### pc!
Changes the turtle's pen color. Takes a single grayscale value, an rgb tuple, or an rgba tuple. Alias: `pencolour!`, `pc!`
```
(color as :keyword)
(gray as :number)
((r as :number, g as :number, b as :number))
((r as :number, g as :number, b as :number, a as :number))
```
[Back to top.](#ludus-prelude-documentation)
---
### pd!
Lowers the turtle's pen, causing it to draw. Alias: `pd!`
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### pencolor
Returns the turtle's pen color as an (r, g, b, a) tuple or keyword. Alias: `pencolour`.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### pencolor!
Changes the turtle's pen color. Takes a single grayscale value, an rgb tuple, or an rgba tuple. Alias: `pencolour!`, `pc!`
```
(color as :keyword)
(gray as :number)
((r as :number, g as :number, b as :number))
((r as :number, g as :number, b as :number, a as :number))
```
[Back to top.](#ludus-prelude-documentation)
---
### pencolour!
Changes the turtle's pen color. Takes a single grayscale value, an rgb tuple, or an rgba tuple. Alias: `pencolour!`, `pc!`
```
(color as :keyword)
(gray as :number)
((r as :number, g as :number, b as :number))
((r as :number, g as :number, b as :number, a as :number))
```
[Back to top.](#ludus-prelude-documentation)
---
### pendown!
Lowers the turtle's pen, causing it to draw. Alias: `pd!`
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### pendown?
Returns the turtle's pen state: true if the pen is down.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### penup!
Lifts the turtle's pen, stopping it from drawing. Alias: `pu!`
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### penwidth
Returns the turtle's pen width in pixels.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### penwidth!
Sets the width of the turtle's pen, measured in pixels. Alias: `pw!`
```
(width as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### pi
No documentation available.
---
### pos?
Returns true if a value is a positive number, otherwise returns false.
```
(x as :number)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### position
Returns the turtle's current position as an `(x, y)` vector tuple.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### pow
Raises a number to the power of another number.
```
(x as :number, y as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### print!
Sends a text representation of Ludus values to the console.
```
(...args)
```
[Back to top.](#ludus-prelude-documentation)
---
### pu!
Lifts the turtle's pen, stopping it from drawing. Alias: `pu!`
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### pw!
Sets the width of the turtle's pen, measured in pixels. Alias: `pw!`
```
(width as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### rad/deg
Converts an angle in radians to an angle in degrees.
```
(a as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### rad/turn
Converts an angle in radians to an angle in turns.
```
(a as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### random
Returns a random something. With zero arguments, returns a random number between 0 (inclusive) and 1 (exclusive). With one argument, returns a random number between 0 and n. With two arguments, returns a random number between m and n. Alternately, given a collection (tuple, list, dict, set), it returns a random member of that collection.
```
()
(n as :number)
(m as :number, n as :number)
(l as :list)
(t as :tuple)
(d as :dict)
```
[Back to top.](#ludus-prelude-documentation)
---
### random_int
Returns a random integer. With one argument, returns a random integer between 0 and that number. With two arguments, returns a random integer between them.
```
(n as :number)
(m as :number, n as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### range
Returns the set of integers between start (inclusive) and end (exclusive) as a list: [start, end). With one argument, starts at 0. If end is less than start, returns an empty list.
```
(end as :number)
(start as :number, end as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### read_input
Waits until there is input in the input buffer, and returns it once there is.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### report!
Prints a value, then returns it.
```
(x)
(msg as :string, x)
```
[Back to top.](#ludus-prelude-documentation)
---
### rest
Returns all but the first element of a list or tuple, as a list.
```
([])
(())
(xs as :list)
(xs as :tuple)
(str as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### right!
Rotates the turtle right, measured in turns. Alias: `rt!`
```
(turns as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### rotate
Rotates a vector by an angle. Default angle measure is turns. An optional keyword argument specifies the units of the angle passed in.
```
((x, y), a)
((x, y), a, units as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### round
Rounds a number to the nearest integer.
```
(n as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### rt!
Rotates the turtle right, measured in turns. Alias: `rt!`
```
(turns as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### second
Returns the second element of a list or tuple.
```
(xs)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### self
Returns the current process's pid, as a keyword.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### send!
Sends a message to the specified process and returns the message.
```
(pid as :keyword, msg)
```
[Back to top.](#ludus-prelude-documentation)
---
### sentence
Takes a list of words and turns it into a sentence.
```
(strs as :list)
```
[Back to top.](#ludus-prelude-documentation)
---
### setheading!
Sets the turtle's heading. The angle is specified in turns, with 0 pointing up. Increasing values rotate the turtle counter-clockwise.
```
(heading as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### show
Returns a text representation of a Ludus value as a string.
```
(x)
```
[Back to top.](#ludus-prelude-documentation)
---
### showturtle!
If the turtle is hidden, shows the turtle. If the turtle is already visible, does nothing.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### sin
Returns the sine of an angle. Default angle measure is turns. An optional keyword argument specifies the units of the angle passed in.
```
(a as :number)
(a as :number, :turns)
(a as :number, :degrees)
(a as :number, :radians)
```
[Back to top.](#ludus-prelude-documentation)
---
### sleep!
Puts the current process to sleep for at least the specified number of milliseconds.
```
(ms as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### slice
Returns a slice of a list or a string, representing a sub-list or sub-string.
```
(xs as :list, end as :number)
(xs as :list, start as :number, end as :number)
(str as :string, end as :number)
(str as :string, start as :number, end as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### slice_n
Returns a slice of a list or a string, representing a sub-list or sub-string.
```
(xs as :list, n as :number)
(str as :string, n as :number)
(xs as :list, start as :number, n as :number)
(str as :string, start as :number, n as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### some
Takes a possibly nil value and a default value. Returns the value if it's not nil, returns the default if it's nil.
```
(nil, default)
(value, _)
```
[Back to top.](#ludus-prelude-documentation)
---
### some?
Returns true if a value is not nil.
```
(nil)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### spawn
Spawns a process. Takes a 0-argument (nullary) function that will be executed as the new process. Returns a keyword process ID (pid) of the newly spawned process.
```
(f as :fn)
```
[Back to top.](#ludus-prelude-documentation)
---
### spawn_turtle
Spawns a new turtle in a new process. Methods on the turtle process mirror those of turtle graphics functions in prelude. Returns the pid of the new turtle.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### split
Takes a string, and turns it into a list of strings, breaking on the separator.
```
(str as :string, splitter as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### sqrt
Returns the square root of a number. Panics if the number is negative.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### sqrt/safe
Returns a result containing the square root of a number, or an error if the number is negative.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### square
Squares a number.
```
(x as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### store!
Stores a value in a box, replacing the value that was previously there. Returns the value.
```
(b as :box, value)
```
[Back to top.](#ludus-prelude-documentation)
---
### string
Converts a value to a string by using `show`. If it is a string, returns it unharmed. Use this to build up strings of different kinds of values.
```
(x as :string)
(x)
(x, ...xs)
```
[Back to top.](#ludus-prelude-documentation)
---
### string?
Returns true if a value is a string.
```
(x as :string)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### strip
Removes punctuation from a string, removing all instances of ,.;:?!
```
("{x},{y}")
("{x}.{y}")
("{x};{y}")
("{x}:{y}")
("{x}?{y}")
("{x}!{y}")
(x)
```
[Back to top.](#ludus-prelude-documentation)
---
### sub
Subtracts numbers or vectors.
```
()
(x as :number)
(x as :number, y as :number)
(x, y, ...zs)
((x1, y1), (x2, y2))
```
[Back to top.](#ludus-prelude-documentation)
---
### tan
Returns the sine of an angle. Default angle measure is turns. An optional keyword argument specifies the units of the angle passed in.
```
(a as :number)
(a as :number, :turns)
(a as :number, :degrees)
(a as :number, :radians)
```
[Back to top.](#ludus-prelude-documentation)
---
### tau
No documentation available.
---
### to_number
Takes a string that presumably contains a representation of a number, and tries to give you back the number represented. Returns a result tuple.
```
(num as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### trim
Trims whitespace from a string. Takes an optional argument, `:left` or `:right`, to trim only on the left or right.
```
(str as :string)
(str as :string, :left)
(str as :string, :right)
```
[Back to top.](#ludus-prelude-documentation)
---
### true?
Returns true if a value is boolean `true`. Useful to distinguish between `true` and anything else.
```
(true)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### tuple?
Returns true if a value is a tuple.
```
(tuple as :tuple)
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### turn/deg
Converts an angle in turns to an angle in degrees.
```
(a as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### turn/rad
Converts an angle in turns to an angle in radians.
```
(a as :number)
```
[Back to top.](#ludus-prelude-documentation)
---
### turtle_commands
No documentation available.
---
### turtle_init
No documentation available.
---
### turtle_state
No documentation available.
---
### type
Returns a keyword representing the type of the value passed in.
```
(x)
```
[Back to top.](#ludus-prelude-documentation)
---
### unbox
Returns the value that is stored in a box.
```
(b as :box)
```
[Back to top.](#ludus-prelude-documentation)
---
### unlink!
Unlinks this process from the other process.
```
(pid as :keyword)
```
[Back to top.](#ludus-prelude-documentation)
---
### unwrap!
Takes a result tuple. If it's :ok, then returns the value. If it's not :ok, then it panics. If it's not a result tuple, it also panics.
```
((:ok, value))
((:err, msg))
```
[Back to top.](#ludus-prelude-documentation)
---
### unwrap_or
Takes a value that is a result tuple and a default value. If it's :ok, then it returns the value. If it's :err, returns the default value.
```
((:ok, value), _)
((:err, _), default)
```
[Back to top.](#ludus-prelude-documentation)
---
### upcase
Takes a string and returns it in all uppercase. Works only for ascii characters.
```
(str as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### update
Takes a dict, key, and function, and returns a new dict with the key set to the result of applying the function to original value held at the key.
```
(d as :dict)
(d as :dict, k as :keyword, updater as :fn)
```
[Back to top.](#ludus-prelude-documentation)
---
### update!
Updates a box by applying a function to its value. Returns the new value.
```
(b as :box, f as :fn)
```
[Back to top.](#ludus-prelude-documentation)
---
### values
Takes a dict and returns a list of values in that dict.
```
(d as :dict)
```
[Back to top.](#ludus-prelude-documentation)
---
### words
Takes a string and returns a list of the words in the string. Strips all whitespace.
```
(str as :string)
```
[Back to top.](#ludus-prelude-documentation)
---
### ws?
Tells if a string is a whitespace character.
```
(" ")
("\t")
("\n")
("\r")
(_)
```
[Back to top.](#ludus-prelude-documentation)
---
### yield!
Forces a process to yield, allowing other processes to execute.
```
()
```
[Back to top.](#ludus-prelude-documentation)
---
### zero?
Returns true if a number is 0.
```
(0)
(_)
```
[Back to top.](#ludus-prelude-documentation)