Palet Scripting Reference

This reference manual describes all the builtin syntax, procedures, and values in the language that are defined by default. --edit docs

Assignment

(define id expr)  Syntax

Set id to the result of expr.

(set! id expr)  Syntax

Set id to the result of expr if id is already defined. If id is not defined, raise an error.

(lambda args body)  Syntax

Produces a procedure that accepts args arguments and runs body when called.

(λ args body)  Syntax

Clone of lambda.

(define/c (proc-bind [id-binding contract]... ))  Syntax

Define a procedure with contracts.

(let ([id val-expr] ...) body))  Syntax

Evaluates the val-exprs left-to-right, creating a new location for each id, and places the values into the locations. It then evaluates the bodys, in which the ids are bound.

(let* ([id val-expr] ...) body))  Syntax

Like let, but evaluates the val-exprs one by one, creating a location for each id as soon as the value is available. The ids are bound in the remaining val-exprs as well as in the body.

Control Flow

(if test-expr then-expr else-expr)  Syntax

Evaluates test-expr. If #t then evaluate then-expr else evaluate else-expr. An error will be raised if evaluated test-expr is not a bool?.

(when test-expr body)  Syntax

Evaluates test-expr. If #t then evaluate body else do nothing. An error will be raised if evaluated test-expr is not a bool?.

(cond [test-clause then-body]... [else then-body])  Syntax

Evaluate each cond-clause if the clause is evaluated to #t then evaluate and return then-body. If the clause is #f continue to the next clause. If there are no clauses left return #<void>. If the last test-clause is else then its evaluated then-body.

(case val-expr case-clause ...)  Syntax

Evaluates val-expr and uses the result to choose a case-clause. The selected clause is the first datum whose quote form is equal? to the result of val-expr. If no case-clause is choosen, then #<void> is returned, unless there is an else clause, in that case return its body.

A case-clause that starts with else must be the last case-clause.

(for ([id seq-expr] ...) body)  Syntax

Loop over seq-expr by setting the variable id to the nth item of seq-expr and evaluating body.

(for-items [key value hash-id] body)  Syntax

Loop over key-value pairs of hash-id, which should be a hash?.

Equality

(equal? v1 v2) → bool?  Procedure

 v1 : any

 v2 : any

Returns #t if v1 and v2 are the same type and have the same value, #f otherwise.

(eq? v1 v2) → bool?  Procedure

 v1 : any

 v2 : any

Returns #t if v1 and v2 refer to the same object in memory, #f otherwise.

Booleans

(bool? v) → bool?  Procedure

 v : any

Returns #t if v is #t or #f, #f otherwise.

true  : bool?  Value

An alias for #t.

false  : bool?  Value

An alias for #f.

Number Predicates

(number? v) → bool?  Procedure

 v : any

Returns #t if v is a number, #f otherwise.

(real? v) → bool?  Procedure

 v : any

Returns #t if v is a real number, #f otherwise.

(int? v) → bool?  Procedure

 v : any

Returns #t if v is an integer, #f otherwise.

(float? v) → bool?  Procedure

 v : any

Returns #t if v is a float, #f otherwise.

(frac? v) → bool?  Procedure

 v : any

Returns #t if v is a fraction (a rational number), #f otherwise.

(complex? v) → bool?  Procedure

 v : any

Returns #t if v is an complex number, #f otherwise.

(nat? v) → bool?  Procedure

 v : any

Returns #t if v is an integer and v is >= 0, #f otherwise.

(nat1? v) → bool?  Procedure

 v : any

Returns #t if v is an integer and v is >= 1, #f otherwise.

(threshold? v) → bool?  Procedure

 v : any

Returns #t if v is a float and v is >= 0 and <= 1, #f otherwise.

(zero? z) → bool?  Procedure

 z : number?

Returns (= z 0)

(positive? x) → bool?  Procedure

 x : real?

Returns (> x 0)

(negative? x) → bool?  Procedure

 x : real?

Returns (< x 0)

(even? n) → bool?  Procedure

 n : int?

Returns (zero? (mod n 2))

(odd? n) → bool?  Procedure

 n : int?

Returns (not (even? n))

Numbers

(+ z ...) → number?  Procedure

 z : number?

Return the sum of zs. Add from left to right. If no arguments are provided the result is 0.

(- z w ...) → number?  Procedure

 z : number?

 w : number?

When no ws are applied return (- 0 z), otherwise return the subtraction of ws of z.

(* z ...) → number?  Procedure

 z : number?

Return the product of zs. If no zs are supplied the result is 1.

(/ z w ...) → number?  Procedure

 z : number?

 w : number?

Returns the division of z by the ws. If no ws are given, returns (/ 1 z).

(div n m ...) → int?  Procedure

 n : int?

 m : int?

Returns the integer division of n by the ms.

(mod n m) → int?  Procedure

 n : int?

 m : int?

Return the modulo of n and m.

(modulo n m) → int?  Procedure

 n : int?

 m : int?

Clone of mod.

(add1 z) → number?  Procedure

 z : number?

Returns (+ z 1).

(sub1 z) → number?  Procedure

 z : number?

Returns (- z 1).

(incf id [delta 1])  Syntax

Mutate id by adding the evaluated result of delta. If delta is not specified, then add 1 to id.

(decf id [delta 1])  Syntax

Mutate id by subtracting the evaluated result of delta. If delta is not specified, then subtract 1 from id.

(= z w ...) → bool?  Procedure

 z : number?

 w : number?

Returns #t if all arguments are numerically equal, #f otherwise.

(< x y) → bool?  Procedure

 x : real?

 y : real?

Returns #t if x is less than y, #f otherwise.

(<= x y) → bool?  Procedure

 x : real?

 y : real?

Returns #t if x is less than or equal to y, #f otherwise.

(> x y) → bool?  Procedure

 x : real?

 y : real?

Returns #t if x is greater than y, #f otherwise.

(>= x y) → bool?  Procedure

 x : real?

 y : real?

Returns #t if x is greater than or equal to y, #f otherwise.

(abs x) → real?  Procedure

 x : real?

Returns the absolute value of x.

(max x ...) → real?  Procedure

 x : real?

Returns largest value of the xs.

(min x ...) → real?  Procedure

 x : real?

Returns smallest value of the xs.

(real-part z) → real?  Procedure

 z : number?

Returns the real part of z.

(imag-part z) → real?  Procedure

 z : number?

Returns the imaginary part of z.

(round x) → int?  Procedure

 x : real?

Returns the closest integer to x resolving ties in favor of even numbers.

(pow z w) → real?  Procedure

 z : real?

 w : real?

Returns z raised to the w power.

(sqrt z) → number?  Procedure

 z : number?

Returns the square root of z.

Modules

(import module)  Syntax

Imports a module

Math Module

Imported with (import math).

(ceil x) → int?  Procedure

 x : real?

Returns the smallest integer bigger than x.

(floor x) → int?  Procedure

 x : real?

Returns the largest integer less than x.

(exp x) → float?  Procedure

 x : real?

Returns Euler's number raised to the x power.

(log x [b]) → float?  Procedure

 x : (and/c real? (>/c 0))

 b : real? = (exp 1)

Returns the natural logarithm of x.
If b is provided it serves as an alternative base.

e  : 2.718281828459045  Value

Euler's number.

(sin z) → float?  Procedure

 z : real?

Returns the sine of z in radians.

(cos z) → float?  Procedure

 z : real?

Returns the cosine of z in radians.

(tan z) → float?  Procedure

 z : real?

Returns the tangent of z in radians.

(asin z) → float?  Procedure

 z : (between/c -1 1)

Returns the arc sine of z.

(acos z) → float?  Procedure

 z : (between/c -1 1)

Returns the arc cosine of z.

(atan z) → float?  Procedure

 z : real?

Returns the arc tangent of z.

pi  : 3.141592653589793  Value

The ratio of a circle's circumference to its diameter.

tau  : 6.283185307179586  Value

The ratio of a circle's circumference to its radius.

Symbols

(symbol? v) → bool?  Procedure

 v : any

Returns #t if v is a symbol, #f otherwise.

(symbol->string sym) → string?  Procedure

 sym : symbol?

Returns a new string whose characters are the same as in sym.

(string->symbol str) → symbol?  Procedure

 str : string?

Returns a symbol whose characters are the same as str.

Strings

(string? v) → bool?  Procedure

 v : any

Returns #t if v is a string, #f otherwise.

(char? v) → bool?  Procedure

 v : any

Returns #t if v is a char, #f otherwise.

(string char ...) → string?  Procedure

 char : char?

Returns a new string from the given chars.

(& str ...) → string?  Procedure

 str : string?

Returns a new string concatenated from the given strs

(&= id delta)  Syntax

Mutate id by concatenating the evaluated result of delta.

(upper str) → string?  Procedure

 str : string?

Returns a string whose characters are the upcase conversion of the characters in str in the C locale.

(lower str) → string?  Procedure

 str : string?

Like upper, but for downcase conversion.

(title str) → string?  Procedure

 str : string?

Like upper, but for titlecase conversion.

(startswith str w) → bool?  Procedure

 str : string?

 w : string?

Returns #t if str starts with w, #f otherwise.

(endswith str w) → bool?  Procedure

 str : string?

 w : string?

Returns #t if str ends with w, #f otherwise.

(strip str) → string?  Procedure

 str : string?

Returns str with its leading and tailing whitespace stripped out.

(replace str w z) → string?  Procedure

 str : string?

 w : string?

 z : string?

Returns str will all ws replaced with zs.

(replace str w z count) → string?  Procedure

 str : string?

 w : string?

 z : string?

 count : int?

Returns str will all ws replaced with zs up until count.

(split str [by]) → vector?  Procedure

 str : string?

 by : string? = " "

Returns a vector of strings split from str by 'by.

(str-repeat str n) → string?  Procedure

 str : string?

 n : int?

Returns str concatenated n times.

(join strs sep) → string?  Procedure

 strs : vector?

 sep : string?

Returns a new string with strs joined between with sep.

(change-file-ext a ext) → string?  Procedure

 a : string?

 ext : string?

Returns a new string with the file extension replaced with ext.

Format

(char->int char) → int?  Procedure

 char : char?

Returns the corresponding int to the given char.

(int->char k) → char?  Procedure

 k : int?

Returns the character corresponding to k.

(number->string z) → string?  Procedure

 z : number?

Returns z as a string.

(~a v ...) → string?  Procedure

 v : any

Converts all vs to a string using display with "" as the joiner.

(~s v ...) → string?  Procedure

 v : any

Converts all vs to a string using display with " " as the joiner.

(~v v ...) → string?  Procedure

 v : any

Converts all v to a string using print with " " as the joiner.

Keywords

(keyword? v) → bool?  Procedure

 v : any

Returns #t if v is a keyword, #f otherwise.

(keyword->string keyword) → vector?  Procedure

 keyword : keyword?

Returns the string from the given keyword, not including the leading hash colon.

(string->keyword str) → keyword?  Procedure

 str : string?

Returns the keyword from the given str.

Lists

(list? v) → bool?  Procedure

 v : any

Return #t if v is a list (an immutable collection of elements), #f otherwise.

(list v ...) → list?  Procedure

 v : any

Returns a new list with the vs as the elements.

(append v ...) → list?  Procedure

 v : list?

Returns a new list with all the arguments appended in order.

Vectors

(vector? v) → bool?  Procedure

 v : any

Returns #t if v is a vector (a mutable, resizable collection of elements), #f otherwise.

(vector v ...) → vector?  Procedure

 v : any

Returns a new vector with the v args filled with its slots in order.

(make-vector size [v]) → vector?  Procedure

 size : nat?

 v : any = 0

Returns a new vector with size slots all filled with vs.

(add! vec v) → void?  Procedure

 vec : vector?

 v : any

Append v to the end of vec.

(pop! vec) → any  Procedure

 vec : vector?

Remove the last element of vec and return it.

(vec-set! vec pos v) → void?  Procedure

 vec : vector?

 pos : int?

 v : any

Set slot pos of vec to v.

(vec-append vec ...) → vector?  Procedure

 vec : vector?

Returns a new vector with all elements of vecs appended in order.

(vec-extend! vec vec2 ...) → void?  Procedure

 vec : vector?

 vec2 : vector?

Modify vec so that all elements of vec2s are appended to the end of vec in order.

(sort vec) → vector?  Procedure

 vec : vector?

Returns a newly sorted vector.

(sort! vec) → void?  Procedure

 vec : vector?

Modifies vec by sorting in place.

(string->vector str) → vector?  Procedure

 str : string?

Returns a new vector with characters contained in str.

Arrays

(array? v) → bool?  Procedure

 v : any

Returns #t if v is an array, #f otherwise.

(array dtype v ...) → array?  Procedure

 dtype : symbol?

 v : any

Returns a freshly allocated array with dtype as its datatype and the v args as its values filled in order.

(make-array dtype size [v]) → array?  Procedure

 dtype : symbol?

 size : nat?

 v : int? = 0

Returns a freshly allocated array with dtype as its datatype and the value v filled.

(array-copy arr) → array?  Procedure

 arr : array?

Returns a new copy of arr.

(array-splice! arr v [start] [stop]) → array?  Procedure

 arr : array?

 v : real?

 start : int? = 0

 stop : int? = (len arr)

Modify arr by setting start to stop into the value v.

(count-nonzero arr) → nat?  Procedure

 arr : array?

Returns the number of non-zeros in arr.

(bool-array? v) → bool?  Procedure

 v : any

Returns #t if v is an array with 'bool as its datatype, #f otherwise.

(bool-array v ...) → bool-array?  Procedure

 v : nat?

Returns a new boolean array with v as its values.

(margin arr left [right]) → bool-array?  Procedure

 arr : bool-array?

 left : int?

 right : int? = left

Returns a new bool-array? with left and right margin applied.

(and first-expr rest-expr ...)  Syntax

Evaluate first-expr if the result is a bool-array? evaluate all rest-exprs and return the logical-and of all arrays. If the result is #f evaluate rest-expr one at a time. Return immediately if any arg is #f, return #t if all values are #t.

(or first-expr rest-expr ...)  Syntax

Evaluate first-expr if the result is a bool-array? evaluate all rest-exprs and return the logical-and of all arrays. If the result is #t evaluate rest-expr one at a time. Return immediately if any arg is #t, return #f if all values are #f.

(xor expr1 expr2) → (or/c bool? bool-array?)  Procedure

 expr1 : (or/c bool? bool-array?)

 expr2 : (or/c bool? bool-array?)

Returns a new boolean or boolean-array based on the exclusive-or of expr1 and expr2. expr2 must be the same type as expr1.

(not h) → (or/c bool? bool-array?)  Procedure

 h : (or/c bool? bool-array?)

Returns the inverse of h.

(mincut arr x) → bool-array?  Procedure

 arr : bool-array?

 x : nat?

Turn all 0 runs that are less than x in length to 1s. 0s represent cuts, 1s represent clips.

(minclip arr x) → bool-array?  Procedure

 arr : bool-array?

 x : nat?

Turn all 1 runs that are less than x in length to 0s

(maxcut arr x) → bool-array?  Procedure

 arr : bool-array?

 x : nat?

Turn all 0 runs that are greater than x in length to 1s

(maxclip arr x) → bool-array?  Procedure

 arr : bool-array?

 x : nat?

Turn all 1 runs that are more than x in length to 0s

all  : 'all  Value

The symbol 'all. Exists for backwards compatibility for older auto-editor versions.

Ranges

(range? v) → bool?  Procedure

 v : any

Returns #t if v is a range object, #f otherwise.

(range start stop [step]) → range?  Procedure

 start : int?

 stop : int?

 step : (and/c int? (not/c 0)) = 1

Returns a range object.

(range->vector rng) → vector?  Procedure

 rng : range?

Returns a new vector based on rng.

Generic Sequences

(iterable? v) → bool?  Procedure

 v : any

Returns #t if v is a vector array string hash or range, #f otherwise.

(sequence? v) → bool?  Procedure

 v : any

Returns #t if v is a vector array string or range, #f otherwise.

(len iter) → nat?  Procedure

 iter : iterable?

Returns the length of iter.

(member x seq) → bool?  Procedure

 x : any

 seq : sequence?

Returns #t if x is in seq, #f otherwise.

(ref seq pos) → any  Procedure

 seq : sequence?

 pos : int?

Returns the element of seq at position pos where the first element is at position 0. Negative positions are allowed.

(reverse seq) → sequence?  Procedure

 seq : sequence?

Returns seq in reverse order.

(slice seq start [stop] [step]) → sequence?  Procedure

 seq : sequence?

 start : int?

 stop : int? = (len seq)

 step : int? = 1

Returns the elements of seq from start inclusively to stop exclusively. If step is negative then stop is inclusive and start is exclusive.

(max-seq v) → any  Procedure

 v : sequence?

Returns the largest value of v.

(min-seq v) → any  Procedure

 v : sequence?

Returns the smallest value of v.

Hashmaps

(hash? v) → bool?  Procedure

 v : any

Returns #t if v is a hash table, #f otherwise.

(hash key val ...) → hash?  Procedure

 key : any

 val : any

Returns a newly constructed hash map from key-value pairs.

(hash-ref hash key) → any  Procedure

 hash : hash?

 key : any

Returns the value for key in hash.

(hash-set! hash key v) → void?  Procedure

 hash : hash?

 key : any

 v : any

Set key to v in hash, overwriting any existing mappings.

(has-key? hash key) → bool?  Procedure

 hash : hash?

 key : any

Returns #t if key is in the hash map, #f otherwise.

(hash-update! hash key updater) → void?  Procedure

 hash : hash?

 key : any

 updater : any

Updates the value mapped by key in hash by applying updater to the value. The value returned by updater becomes the new mapping for key, overwriting the original value in hash.

(hash-remove! hash key) → void?  Procedure

 hash : hash?

 key : any

Removes any existing mapping for key in hash.

I/O

(output-port? v) → bool?  Procedure

 v : any

Returns #t if v is an output port, #f otherwise.

(open-output-file path) → output-port?  Procedure

 path : string?

Opens the file specified by path for output.

(close-port out) → void?  Procedure

 out : output-port?

Flushes, then closes the port out. The port is now incapable of delivering or accepting data. This has no effect if the port has already been closed.

(closed? v) → bool?  Procedure

 v : output-port?

Returns #t if v is a closed port, #f otherwise.

Printing

(print datum [out]) → void?  Procedure

 datum : any

 out : output-port? = #<stdout>

Display datum like REPL does.

(println datum [out]) → void?  Procedure

 datum : any

 out : output-port? = #<stdout>

Display datum like print but with a newline character.

(display datum [out]) → void?  Procedure

 datum : any

 out : output-port? = #<stdout>

Display datum to stdout.

(displayln datum [out]) → void?  Procedure

 datum : any

 out : output-port? = #<stdout>

Display datum to stdout with a newline character.

Actions

(begin datum ...) → any  Procedure

 datum : any

Evaluates all arguments and returns the last one.

(assert expr [msg]) → void?  Procedure

 expr : any

 msg : (or/c string? #f) = #f

Raises error if expr is not #t.

(error msg) → void?  Procedure

 msg : string?

Raises an exception with msg as the message.

(sleep time) → void?  Procedure

 time : (or/c int? float?)

Adds a delay of time seconds.

(system cmd) → bool?  Procedure

 cmd : string?

Executes a Unix-like or Windows shell command synchronously. Returns #t if successful, #f otherwise.

(system* command arg ...) → bool?  Procedure

 command : string?

 arg : string?

Executes a Unix-like or Windows shell command synchronously. Returns #t if successful, #f otherwise.

Void

(void? v) → bool?  Procedure

 v : any

Returns #t if v is #<void>, #f otherwise.

(void v ...) → void?  Procedure

 v : any

Returns the constant #<void>. All v arguments are ignored.

Procedures

(procedure? v) → bool?  Procedure

 v : any

Returns #t if v is a procedure, #f otherwise.

(contract? v) → bool?  Procedure

 v : any

Returns #t if v is a contract, #f otherwise. A contract is either a bool?, number?, string?, symbol? literal, or a predicate.

(any v) → bool?  Procedure

 v : any

Always returns #t regardless of the value of v.

(map proc seq) → sequence?  Procedure

 proc : procedure?

 seq : sequence?

Returns a new sequence with the results of proc applied to each element.

(apply proc seq) → any  Procedure

 proc : procedure?

 seq : sequence?

Applies proc given seq as its arguments.

(and/c contract) → contract?  Procedure

 contract : contract?

Takes any number of contracts and returns a contract predicate that accept values that satisfies all contracts.

(or/c contract) → contract?  Procedure

 contract : contract?

Takes any number of contracts and returns a contract predicate that accept values that can satisfy one or more contracts.

(not/c contract) → contract?  Procedure

 contract : contract?

Takes one contract and returns a contract that accepts a value that satisfies the opposite of the given contract.

(>=/c n) → contract?  Procedure

 n : real?

Returns a new contract that requires the input to be real? and >= than n.

(>/c n) → contract?  Procedure

 n : real?

Like >=/c but for >.

(<=/c n) → contract?  Procedure

 n : real?

Like >=/c but for <=.

(</c n) → contract?  Procedure

 n : real?

Like >=/c but for <.

(between/c n m) → contract?  Procedure

 n : real?

 m : real?

Returns a contract that requires the input a real number between n and m or equal to one of them.

Reflection

(quote body)  Syntax

Returns body as its "literalized" form, a constant value with its binding names copied.

(var-exists? sym) → bool?  Procedure

 sym : symbol?

Returns #t if the variable corresponding to sym is defined in the current environment, #f otherwise.

(rename original new)  Syntax

Move the original identifier to the new identifier. Works for both procedures and syntaxes.

(delete identifier)  Syntax

Deletes identifier from the environment.

(eval arg) → any  Procedure

 arg : any

Allows running arbitrary, dynamically generated code. Effective the opposite of quote.

Objects

(class name [id contract?]...)  Syntax

Creates a object initiator and a predicate to identify that object. Example:

(class person [name string?] [age nat?])
(define alyssa (person "Alyssa P." 23))
alyssa.name  ; returns "Alyssa P."
alyssa.age   ; returns 23
(person? alyssa)  ; returns #t
Class objects are mutable
(incf alyssa.age)  ; Increase age by 1

(@r obj attr)  Syntax

Returns the specified attribute on the object. Users are not expected to directly use this special form. Instead, use the `.` syntax.

(define a 10)
a.incf ; turns into (@r a incf)