Skip to content

Function Reference

Silvio Mayolo edited this page Jun 7, 2017 · 34 revisions

Function Reference

All of the possible methods of calling a function are listed here. If a method is not listed, it should be considered undefined behavior. For instance, passing no arguments to the = operator is undefined behavior.

For all functions except those under the "Special Operators" section, it is assumed that the standard applicative evaluation order of arguments is applied. That is, after the function itself is evaluated, each argument is evaluated in order, first to last, before being passed in to the function.

Special Operators

Important: Special operators are NOT functions. They do not follow the normal evaluation rules. If a special operator is ever treated as a value, the consequences are undefined.

=

(= y)

The value y is evaluated and assigned to the variable %.

(= x y)

The value y is evaluated and assigned to the variable indicated by x, which is not evaluated. If x is not an atom, y is evaluated and discarded.

(= xs ... y)

The value y is evaluated once and assigned to all of the variables given. Any non-atom variables are ignored.

quote / q

(quote expr)

The expression is returned, unevaluated. The quote operator has two forms of syntax sugar attributed to it. Like in most Lisps, a single quote preceding an expression like 'expr is equivalent to (quote expr). Additionally, in ShinyLisp, square brackets will quote the operand, so [foo bar baz] is equivalent to (quote (foo bar baz)).

cond / i

(cond args ...)

cond is the general form of an if-statement. The operator considers its arguments in pairs. The first of each pair is evaluated. If it returns a truthy value, the second of the pair is evaluated and returned. Otherwise, the next pair is considered. If there are an odd number of arguments, the final argument is considered an "else" clause and is returned if none of the conditions match. If there is no "else" clause and no conditions match, the value () is returned.

Programmers from other languages should think of cond as an if-else-if statement. For example, the expression (cond c1 e1 c2 e2 default) is equivalent to if c1 then e1 elsif c2 then e2 else default end in Ruby.

Arithmetic

p

(p xs ...)

All of the expressions are coerced into numbers and summed together. If no expressions are provided, 0 is returned. Note that p (short for "plus") is used for addition. The usual symbol + is a capital =, which is used for assignment.

m

(m)

The m function with no arguments returns the value -1.

(m x)

The value x is coerced to a number and negated. So -x, the additive inverse of x, is returned.

(m x ys ...)

All of the arguments are coerced to numbers, then each of ys is subtracted from x. Note that m (short for "minus") is used for subtraction to parallel the use of p for addition.

&

(& xs ...)

The arguments are coerced together and their product is returned. If no arguments are supplied, 1 is returned. Note that & is the lowercase form of *, which is used in most languages for multiplication.

/

(/ x)

Currently, passing zero or one argument to / is undefined behavior, as the language does not yet support floating point values. Eventually, passing a single argument to / will yield the reciprocal. Do not yet rely on this behavior.

(/ x ys ...)

All of the arguments are coerced to numbers. Then each of ys is divided in turn by x, yielding the final value. If any of the divisors is equal to zero, the dividend x is returned. Currently, division truncates toward negative infinity, but floating point numbers may be supported in the future.

Lists and Sequences

list / l

(list exprs ...)

All of the expressions given are put into a list, which is returned.

take / tk / car

(take)

Returns 10,000

(take xs)

If xs is a cons cell, its car is returned. Otherwise, xs itself is returned. On a proper list, this has the effect of returning the first element.

(take n xs)

Returns the first n elements of the list xs. The value n is coerced to a number. If xs is not a list then xs is returned. If xs is not long enough to support the operation, the entire list is once again returned.

(take n m ... xs)

Returns a list of results, containing the first n elements of the list, followed by the next m, and so on. Like with the other forms of take, if repeated application ever yields a non-cons value, including (), the value itself is returned.

drop / dp / cdr

(drop)

Returns the value 2,048

(drop xs)

Returns the cdr of xs if xs is a cons cell, or returns xs itself otherwise. On a proper list, this has the effect of dropping the first element.

(drop n xs)

After coercing n to a number, returns the list xs with the first n elements dropped. If xs is not a cons cell, then xs itself is returned. If xs is too short, then the final cdr of xs is returned.

(drop n m ... xs)

Calling drop with three or more arguments is equivalent to calling it with each index separately. That is, (drop n m k xs) is equivalent to (list (drop n xs) (drop m xs) (drop k xs) with the caveat that the arguments are evaluated in standard applicative order and xs is only evaluated once total.

map / a

(map)

Returns the uppercase alphabet, as a string.

(map xs)

Coerces the argument to a list and reverses it.

(map f xs ...)

Zips the list arguments together, coercing as necessary, and then applies f to each argument list in the resulting zipped list. When any one of the arguments terminates, the zipped list terminates. A list of the results is returned.

filter / e

(filter)

Returns the lowercase alphabet, as a string.

(filter xs)

Returns the argument, coerced to a list, with all falsy elements removed.

(filter f xs)

Coerces the xs argument to a list and removes all elements of the list for which f returns falsy when called.

sequence / sq

(sequence)

If no arguments are passed to sequence, it returns (sequence 0).

(sequence n)

Returns a function representing a sequence. The returned function shall take one argument, an 0-based index K, and return the Kth element in the sequence. If N is not passed, it defaults to zero. Any further arguments to the returned function are evaluated but ignored. See below for a list of sequences available.

(sequence n y ys ...)

If any additional arguments are passed to sequence, they are evaluated but ignored. However, the behavior changes if at least two arguments are passed. A sequence function is still returned. However, in this case, when the returned function is called with index K, the first K elements of the sequence are returned in a list. If K is not supplied, it defaults to one. Any further arguments are evaluated but ignored. See below for a list of sequences available.

The following are the valid values for n. The value n is always coerced to a number. If a negative number is passed, a function representing the sequence of () infinitely repeated is returned. If an invalid positive number is passed, the behavior is undefined.

  • 0 - The Fibonacci sequence 0 1 1 2 3 5 8 13 ...
  • 1 - The 2^n sequence 1 2 4 8 16 32 64 128 ...
  • 2 - The identity sequence 0 1 2 3 4 5 6 7 ...
  • 3 - The prime numbers 2 3 5 7 11 13 17 19 ...

Control Flow

progn / pgn

(progn exprs ...)

The value of the last expression is returned. If progn is called with no arguments, it returns (). This function is useful for concisely sequencing multiple expressions together. Note that curly braces act as syntax sugar for progn, so {foo bar baz} is equivalent to (progn foo bar baz).

apply / ap

(apply)

Returns 2,147,483,647, the largest value that can fit into a 32-bit unsigned integer.

(apply f)

Applies the function f to itself. That is, (apply f) is equivalent to (f f) except that f is only evaluated once.

(apply f x y ... z xs)

Every argument from the second to the penultimate argument is collected into a list and prepended onto the final argument, which must be a proper list. Then f is called with the argument list that was created from the arguments.

Comparisons

==

(==)

Returns the constant 100.

(== x)

Returns the constant 1,000. Note that the argument is still evaluated.

(== x ys ...)

Returns 1 if the values are all equal and 0 otherwise. Values are equal if and only if they have the same type and the same underlying data. Numbers, atoms, and strings are compared for equality in the usual way. The nil value () is equal to itself and only itself. Cons cells are equal if and only if their corresponding components are equal.

All other values, such as built-in functions and special operators, currently compare unequal to everything, even to themselves. This behavior may change. It is not recommended to pass built-in operators to ==.

Input / Output

puts / pu

(puts exprs ...)

Outputs each expression to stdout on a separate line. This function always returns ().

Miscellaneous

less-than / c,

less-eq / =,

greater-than / c;

greater-eq / =;

str-less-than / ,c

str-less-eq / ,=

str-greater-than / ;c

str-greater-eq / ;=

range / rg

quit

cons / c

bitand / b&

bitor / b;

bitxor / b%

boolnorm / bn

hook / hk

id / d

compose / ,

sort / st

divides / //

foldl / fl

foldr / fr

join / jn

mod / md

even / ev

odd / od

strings / s

prime / pm

up

mo

po

mt

pt

split / sp

inter / ps

insert / it

remove / rm

nth / nh

read / rd

eval / el

two-curry / tc

seq-while / sw

print-greek / pgk

each-char / ec

string-replace / sr

o

w

t

n

uc

ucx

lc

lcx

Clone this wiki locally