Palet Scripting Reference

This manual describes the complete overview of the palet scripting language.

Palet is an anagram-acronym of the words: (A)uto-(E)ditor's (T)iny (P)rocedural (L)anguage

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.

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?.

(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.

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.

(uint? v) → bool?  Procedure

 v : any?

Returns #t if v is an integer and v is greater than -1, #f otherwise.

(nat? v) → bool?  Procedure

 v : any?

Returns #t if v is an integer and v is greater than 0, #f otherwise.

(float? v) → bool?  Procedure

 v : any?

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

(threshold? v) → bool?  Procedure

 v : any?

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

(frac? v) → bool?  Procedure

 v : any?

Returns #t if v is a fraction (a rational number), #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?

When no ws are applied return (/ 1 z). Otherwise return the division of ws of z.

(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).

(= 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.

(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.

Exponents

(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.

(exp x) → float?  Procedure

 x : real?

Returns Euler's number raised to the x power.

(log x [b]) → float?  Procedure

 x : real?

 b : real? = (exp 1)

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

Geometry

(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.

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.

(string-append str ...) → string?  Procedure

 str : string?

Returns a new string concatenated from the given strs

(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 : datum?

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

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

 v : datum?

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

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

 v : datum?

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

Vectors

(vector? v) → bool?  Procedure

 v : any?

Returns #t if v is a vector, #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 : uint?

 v : any = 0

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

(vector-pop! vec) → any  Procedure

 vec : vector?

Remove the last element of vec and return it.

(vector-add! vec v)  Procedure

 vec : vector?

 v : any

Append v to the end of vec.

(vector-set! vec pos v)  Procedure

 vec : vector?

 pos : int?

 v : any

Set slot pos of vec to v.

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

 vec : vector?

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

(vector-extend! vec vec2 ...)  Procedure

 vec : vector?

 vec2 : vector?

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

(string->vector str) → vector?  Procedure

 str : string?

Returns a new string filled with the characters of 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 : uint?

 v : int? = 0

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

(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) → uint?  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 : uint?

Returns a new boolean array with v as its values.

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

 left : int?

 right : int? = left

 arr : bool-array?

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 : uint?

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

(minclip arr x) → bool-array?  Procedure

 arr : bool-array?

 x : uint?

Turn all 1s sections that are less 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 bytes hash pair or range, #f otherwise.

(sequence? v) → bool?  Procedure

 v : any?

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

(len seq) → uint?  Procedure

 seq : iterable?

Returns the length of seq.

(ref seq pos) → any  Procedure

 seq : iterable?

 pos : int?

Returns the element of seq at position pos where the first element is at position 0. For sequences other than pair?, negative positions are allowed.

(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.

(reverse seq) → sequence?  Procedure

 seq : sequence?

Returns seq in reverse order.

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.

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

 hash : hash?

 key : any

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

Actions

(begin datum ...) → any  Procedure

 datum : any

Evaluates all arguments and returns the last one.

(display datum) → void?  Procedure

 datum : any

Display datum to stdout.

(displayln datum) → void?  Procedure

 datum : any

Display datum to stdout with a newline character.

(print datum) → void?  Procedure

 datum : any

Display datum like REPL does.

(println datum) → void?  Procedure

 datum : any

Display datum like REPL does with a newline character.

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?, 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 accepts any values that satisfies all contracts.

(or/c contract) → contract?  Procedure

 contract : contract?

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

(not/c contract) → contract?  Procedure

 contract : contract?

Takes one contract and returns a contract that accepts one value and 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 <.

Objects

(object? v) → bool?  Procedure

 v : any?

Returns #t if v is an object, #f otherwise. Anything that's not an object is a primitive.

(attrs obj) → vector?  Procedure

 obj : object?

Returns all attributes of obj as a vector of strings.

(@r obj attr)  Syntax

Returns the specified attribute on the object.

Reflection

(quote body)  Syntax

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