Slang Library Reference

by Sean P. Miller


Contents

std.alg
std.basic
std.io
std.math
std.rand
std.sys
std.text
std.thread

std.alg

The definitions in this file are designed to fulfill the following purposes:

Functions

Signature Description
filter (pred: `a -> bool, seq: [`a]): [`a] Collects all elements in seq that satisfy pred.
foldl (f: `a * `b -> `a, init: `a, seq: [`b]): `a Left-to-right accumulation of seq.
foldr (f: `a * `b -> `b, init: `b, seq: [`a]): `b Right-to-left accumulation of seq.
foreach (f: `a -> void, seq: [`a]): void Applies f to each element in seq.
map (f: `a -> `b, seq: [`a]): [`b] Returns the results of f applied to each element in seq.
zip (xs: [`a], ys: [`b]): [`a * `b] Pairs corresponding elements of xs and ys.
partition (pred: `a -> bool, seq: [`a]): [`a] * [`a] Returns a pair of sequences (T,F) where T contains the elements in seq that satisfy pred and F contains the rest.
generate (gen: void -> `a, count: int): [`a] Creates a sequence of length count initialized with calls to gen.
range (lower: int, upper: int): [int] Returns all integers in the half-open range [lower, upper).
mapMaybe (f: `a -> `b maybe, seq: [`a]): [`b] A specialized transform that discards Nothing results.
merge (cmp: `a * `a -> order, left: [`a], right: [`a]): [`a] Merges two sequences into ascending order.
mergeSort (cmp: `a * `a -> order, seq: [`a]): [`a] A stable, safe, slower sort.
quickSort (cmp: `a * `a -> order, seq: [`a]): [`a] An unstable, unsafe, faster sort.
find (pred: `a -> bool, seq: [`a]): `a maybe Attempts to find an element in seq that satisfies pred.
findAll (pred: `a -> bool, seq: [`a]): [`a] Collects all elements in seq that satisfy pred.
binarySearch (cmp: `a * `a -> order, e: `a, seq: [`a]): `a maybe Fast lookup for presorted sequences.

filter

filter (pred: `a -> bool, seq: [`a]): [`a]

(* r1 contains ss with empty strings removed *)
def r1 = filter (lambda (s) { return s <> ""; }, ss);

Parameters:

Returns:


foldl

foldl (f: `a * `b -> `a, init: `a, seq: [`b]): `a

Parameters:

Returns:

Notes:


foldr

foldr (f: `a * `b -> `b, init: `b, seq: [`a]): `b

Parameters:

Returns:

Notes:


foreach

foreach (f: `a -> void, seq: [`a]): void

Parameters:

Notes:


map

map (f: `a -> `b, seq: [`a]): [`b]

Parameters:

Returns:


zip

zip (xs: [`a], ys: [`b]): [`a * `b]

Parameters:

Returns:

Notes:


partition

partition (pred: `a -> bool, seq: [`a]): [`a] * [`a]

Parameters:

Returns:


generate

generate (gen: void -> `a, count: int): [`a]

Parameters:

Returns:


range

range (lower: int, upper: int): [int]

Parameters:

Returns:

Notes:


mapMaybe

mapMaybe (f: `a -> `b maybe, seq: [`a]): [`b]

(* r1 will be [1,2,4] *)
def r1 = mapMaybe (str2int, ["1", "2", "xx", "4"]);

Parameters:

Returns:

Notes:


merge

merge (cmp: `a * `a -> order, left: [`a], right: [`a]): [`a]

Parameters:

Returns:

Notes:


mergeSort

mergeSort (cmp: `a * `a -> order, seq: [`a]): [`a]

Parameters:

Returns:

Notes:


quickSort

quickSort (cmp: `a * `a -> order, seq: [`a]): [`a]

Parameters:

Returns:

Notes:


find

find (pred: `a -> bool, seq: [`a]): `a maybe

Parameters:

Returns:

Notes:


findAll

findAll (pred: `a -> bool, seq: [`a]): [`a]

Parameters:

Returns:

Notes:


binarySearch

binarySearch (cmp: `a * `a -> order, e: `a, seq: [`a]): `a maybe

Parameters:

Returns:

Notes:

std.basic

The definitions in this file are designed to fulfill the following purposes:

Generic exceptions, maybe, and order are also provided.

Note that this file is automatically included in other headers.

Constructors

Data Constructor Type Constructor Parameterization Description
Arithmetic BasicException string Thrown when various mathematical errata occur.
OutOfBounds BasicException string Thrown when a numeric argument is outside expected bounds.
Empty BasicException string Thrown when an empty sequence is unacceptable.
BadArgument BasicException string Thrown when an argument is malformed.
RuntimeFailure BasicException string Thrown when an internal error occurs.
Interrupted BasicException string Thrown when blocked operation is interrupted.
StackOverflow BasicException string Thrown when stack exhausted.
Existential BasicException Thrown when Nothing is unacceptable.
Nothing `a maybe Represents a failed computation.
Some `a maybe `a Wraps the result of a successful computation.
Less order Less-than comparison result.
Equal order Equal-to comparison result.
Greater order Greater-than comparison result.

Functions

Signature Description
int2str (n: int): string Produces a human-readable base-ten string from n.
int2radix (n: int, radix: int): string Produces a human-readable base-radix string from n.
int2char (n: int): char maybe Interprets n as a Unicode codepoint.
real2str (x: real): string Produces a human-readable base-ten string from x.
getprec (x: real): int Returns x's precision.
setprec (x: real, prec: int): real Creates a new real with x's value and precision prec.
float2str (f: float): string Produces a human-readable base-ten string from f.
str2int (s: string): int maybe Converts base-ten string s into an integer.
str2real (s: string): real maybe Converts s into a real.
str2float (s: string): float maybe Converts s into a float.
sub (s: string, index: int): char Returns the character from s at position index.
substr (s: string, index: int, count: int): string Returns a substring of s of length count beginning at position index.
strlen (s: string): int Returns the number of characters in s.
explode (s: string): [char] Converts s into an equivalent sequence of characters.
char2str (c: char): string Converts c into an equivalent string.
char2int (c: char): int Returns an integer equal to c's Unicode codepoint.
bool2str (b: bool): string Produces a human-readable string from b.
head (seq: [`a]): `a Returns seq's leftmost element.
tail (seq: [`a]): [`a] Returns seq without its leftmost element.
rhead (seq: [`a]): `a Returns seq's rightmost element.
rtail (seq: [`a]): [`a] Returns seq without its rightmost element.
len (seq: [`a]): int Returns the number of elements in seq.
implode (charSeq: [char]): string Converts charSeq into an equivalent string.
split (index: int, seq: [`a]): [`a] * [`a] Returns (L,R) where L is all elements in seq before position index and R is those at or after index.
at (index: int, seq: [`a]): `a Returns the element in seq at position index.
take (count: int, seq: [`a]): [`a] Returns the first count elements of seq.
drop (count: int, seq: [`a]): [`a] Discards the first count elements of seq and returns the rest.
insert (index: int, e: `a, seq: [`a]): [`a] Returns seq with element e inserted at position index.
delete (index: int, seq: [`a]): [`a] Returns seq without the element at position index.
slice (begin: int, end: int, seq: [`a]): [`a] Returns the subsequence of seq from position begin to position end, inclusive.
rev (seq: [`a]): [`a] Returns seq in reverse order.
val (e: `a maybe): `a Extracts the argument from a maybe construction.
isNothing (e: `a maybe): bool If given Nothing, returns true; else, returns false.
isSome (e: `a maybe): bool If given any Some construction, returns true; else, returns false.
compare (a: `a, b: `a): order Defines a total ordering over most types.
send (ch: `a channel, msg: `a): void Queues msg in FIFO ch.
recv (ch: `a channel): `a Pulls the first available message from ch.

int2str

int2str (n: int): string

Parameters:

Returns:


int2radix

int2radix (n: int, radix: int): string

Parameters:

Returns:

Exceptions:


int2char

int2char (n: int): char maybe

Parameters:

Returns:

Notes:


getprec

getprec (x: real): int

Parameters:

Returns:


setprec

setprec (x: real, prec: int): real

Parameters:

Returns:

Exceptions:

Notes:


float2str

float2str (f: float): string

Parameters:

Returns:


str2int

str2int (s: string): int maybe

Parameters:

Returns:


str2real

str2real (s: string): real maybe

Parameters:

Returns:


str2float

str2float (s: string): float maybe

Parameters:

Returns:


sub

sub (s: string, index: int): char

Parameters:

Returns:

Exceptions:

Notes:


substr

substr (s: string, index: int, count: int): string

Parameters:

Returns:

Exceptions:

Notes:


strlen

strlen (s: string): int

Parameters:

Returns:

Notes:


explode

explode (s: string): [char]

Parameters:

Returns:

Notes:


char2str

char2str (c: char): string

Parameters:

Returns:


char2int

char2int (c: char): int

Parameters:

Returns:

Notes:


bool2str

bool2str (b: bool): string

Parameters:

Returns:


head (seq: [`a]): `a

Parameters:

Returns:

Exceptions:


tail

tail (seq: [`a]): [`a]

Parameters:

Returns:

Exceptions:


rhead

rhead (seq: [`a]): `a

Parameters:

Returns:

Exceptions:


rtail

rtail (seq: [`a]): [`a]

Parameters:

Returns:

Exceptions:


len

len (seq: [`a]): int

Parameters:

Returns:

Notes:


implode

implode (charSeq: [char]): string

Parameters:

Returns:

Notes:


split

split (index: int, seq: [`a]): [`a] * [`a]

Parameters:

Returns:

Exceptions:

Notes:


at

at (index: int, seq: [`a]): `a

Parameters:

Returns:

Exceptions:

Notes:


take

take (count: int, seq: [`a]): [`a]

Parameters:

Returns:

Exceptions:

Notes:


drop

drop (count: int, seq: [`a]): [`a]

Parameters:

Returns:

Exceptions:

Notes:


insert

insert (index: int, e: `a, seq: [`a]): [`a]

Parameters:

Returns:

Exceptions:

Notes:


delete

delete (index: int, seq: [`a]): [`a]

Parameters:

Returns:

Exceptions:

Notes:


slice

slice (begin: int, end: int, seq: [`a]): [`a]

Parameters:

Returns:

Exceptions:

Notes:


rev

rev (seq: [`a]): [`a]

Parameters:

Returns:

Notes:


val

val (e: `a maybe): `a

Parameters:

Returns:

Exceptions:


isNothing

isNothing (e: `a maybe): bool

Parameters:

Returns:


isSome

isSome (e: `a maybe): bool

Parameters:

Returns:


compare

compare (a: `a, b: `a): order

Parameters:

Returns:

Exceptions:

Notes:


send

send (ch: `a channel, msg: `a): void

Parameters:

Notes:


recv

recv (ch: `a channel): `a

Parameters:

Returns:

Exceptions:

Notes:

std.io

The definitions in this file are designed to fulfill the following purposes:

Constructors

Data Constructor Type Constructor Parameterization Description
FileNotFound IOException string Thrown when file cannot be found.
IOFailure IOException string Thrown when unspecified I/O error occurs.
BadFd IOException string Thrown when given invalid file descriptor.
BadOpenmode IOException string Thrown when given invalid openmode.

Type Aliases

Name Value
fd {loc: string, mode: openmode, id: int}
openmode int

Constants

Definition Value
MODE_IN: openmode 1
MODE_OUT: openmode 1 << 1
MODE_APP: openmode 1 << 2
MODE_TRUNC: openmode 1 << 3
STDIN: fd {loc="<STDIN>", mode=MODE_IN, id=0}
STDOUT: fd {loc="<STDOUT>", mode=MODE_OUT, id=1}
STDERR: fd {loc="<STDERR>", mode=MODE_OUT, id=2}

Functions

Signature Description
print (s: string): void Writes s to STDOUT.
getline (): string Reads all characters up to a newline from STDIN.
open (fileName: string, mode: openmode): fd Opens the file identified by fileName.
close (des: fd): void Closes a file that has already been opened.
eof (des: fd): bool Returns true if the file stream is at the end.
read (des: fd, count: int): string Reads count characters from a file stream.
write (des: fd, data: string): void Writes data to a file stream.

print

print (s: string): void

Parameters:

Notes:


getline

getline (): string

Notes:


open

open (fileName: string, mode: openmode): fd

try {
    def is = open ("some_file.txt", MODE_IN);

    try {
        (* read from the file *)
    } catch (e: IOException) {
        (* handle read errors *)
    } finally {
        close is;
    }
} catch (e: IOException) {
    (* handle open failure *)
}

Parameters:

Returns:

Exceptions:

Notes:


close

close (des: fd): void

Parameters:

Exceptions:

Notes:


eof

eof (des: fd): bool

Parameters:

Returns:

Exceptions:


read

read (des: fd, count: int): string

Parameters:

Returns:

Exceptions:

Notes:


write

write (des: fd, data: string): void

Parameters:

Exceptions:

Notes:

std.math

Functions

Signature Description
abs (n: int): int Absolute value.
promote (n: int): real Converts an int to a real (avoid if possible).
gcd (a: int, b: int): int Greatest common divisor.
extEuclid (a: int, b: int): int * int * int Extended Euclidean algorithm.
lcm (a: int, b: int): int Least common multiple.
max (a: int, b: int): int The larger of two arguments.
min (a: int, b: int): int The smaller of two arguments.
powMod (base: int, exponent: int, p: int): int baseexponent (mod p)
invMod (a: int, m: int): int Modular multiplicative inverse.
nextProbablePrime (n: int, trials: int): int Finds the smallest prime larger than n with probability (1 - 0.25trials).
isProbablePrime (n: int, trials: int): int Determines whether n is prime with probability (1 - 0.25trials).
div3 (dividend: int): int Fast routine to find quotient of dividend / 3.
mod3 (n: int): int Fast routine to find n (mod 3).
ceil (x: real): int Finds smallest integer greater than x.
floor (x: real): int Finds largest integer less than x.
round (x: real): int Returns the integer closest to x.
pi (precision: int): real Calculates precision digits of the number π.
sqrt (x: real): real Returns the principal (nonnegative) square root of x.
exp (x: real): real ex
log (x: real): real ln x
sin (x: real): real sin x
cos (x: real): real cos x
tan (x: real): real tan x
fpromote (n: int): float Converts an int to a float.
isNaN (f: float): bool Returns true if f is not-a-number.
isInf (f: float): bool Returns true if f is either positive or negative infinity.
fsqrt (f: float): float Float-valued principal square root.
modf (f: float): int * float Decomposes argument into integer and fraction parts.

abs

abs (n: int): int

Parameters:

Returns:


promote

promote (n: int): real

Parameters:

Returns:

Notes:


gcd

gcd (a: int, b: int): int

Parameters:

Returns:

Notes:


extEuclid

extEuclid (a: int, b: int): int * int * int

Parameters:

Returns:

Notes:


lcm

lcm (a: int, b: int): int

Parameters:

Returns:

Notes:


max

max (a: int, b: int): int

Parameters:

Returns:


min

max (a: int, b: int): int

Parameters:

Returns:


powMod

powMod (base: int, exponent: int, p: int): int

Parameters:

Returns:

Exceptions:


invMod

invMod (a: int, m: int): int

Parameters:

Returns:

Exceptions:


nextProbablePrime

nextProbablePrime (n: int, trials: int): int

Parameters:

Returns:

Exceptions:

Notes:


isProbablePrime

isProbablePrime (n: int, trials: int): int

Parameters:

Returns:

Exceptions:

Notes:


div3

div3 (dividend: int): int

Parameters:

Returns:

Notes:


mod3

mod3 (n: int): int

Parameters:

Returns:

Notes:


ceil

ceil (x: real): int

Parameters:

Returns:


floor

floor (x: real): int

Parameters:

Returns:


round

round (x: real): int

Parameters:

Returns:

Notes:


pi

pi (precision: int): real

Parameters:

Returns:

Exceptions:


sqrt

sqrt (x: real): real

Parameters:

Returns:

Exceptions:


exp

exp (x: real): real

Parameters:

Returns:


log

log (x: real): real

Parameters:

Returns:

Exceptions:


sin

sin (x: real): real

Parameters:

Returns:


cos

cos (x: real): real

Parameters:

Returns:


tan

tan (x: real): real

Parameters:

Returns:


fpromote

fpromote (n: int): float

Parameters:

Returns:

Exceptions:


isNaN

isNaN (f: float): bool

Parameters:

Returns:


isInf

isInf (f: float): bool

Parameters:

Returns:


fsqrt

fsqrt (f: float): float

Parameters:

Returns:

Exceptions:


modf

modf (f: float): int * float

Parameters:

Returns:

std.rand

The definitions in this file are designed to fulfill the following purposes:

Functions

Signature Description
rand (n: int): int Generates a random integer in the closed range [0, n * 230].
randPrime (n: int): int Generates a random prime integer guaranteed to be n bytes in size.

rand

rand (n: int): int

Parameters:

Returns:

Notes:


randPrime

randPrime (n: int): int

Parameters:

Returns:

Notes:

std.sys

Functions

Signature Description
coresAvailable (): int Returns the number of CPU cores currently available to the runtime.

coresAvailable

coresAvailable (): int

Returns:

Notes:

std.text

The definitions in this file are designed to fulfill the following purposes:

Functions

Signature Description
join (delimiter: string, words: [string]): string Concatenates the elements of words, separated by delimiter, into a single string.
rjust (width: int, fill: char, s: string): string Right-justifies s in a field width characters wide.
ljust (width: int, fill: char, s: string): string Left-justifies s in a field width characters wide.
fformat (form: string, f: float): string Formats f according to format form.

join

join (delimiter: string, words: [string]): string

Parameters:

Returns:


rjust

rjust (width: int, fill: char, s: string): string

Parameters:

Returns:


ljust

ljust (width: int, fill: char, s: string): string

Parameters:

Returns:


fformat

fformat (form: string, f: float): string

Parameters:

Returns:

Exceptions:

Notes:

std.thread

This header provides an interface to a simple thread-management system.

Constructors

Data Constructor Type Constructor Parameterization Description
BadThreadId ThreadException string Thrown on invalid thread id.

Functions

Signature Description
isRunning (threadId: string): bool Returns true if the specified thread is running; else, returns false
wait (threadId: string): void Blocks this thread until the specified thread terminates, then reclaims its resources.
self (): string Returns this thread's id.
sleep (msec: int): void Suspends this thread's execution for at least msec milliseconds.
spawn (threadId: string, arg: `a, f: `a -> void): void Spins off a new thread named threadId beginning execution with the function call (f arg).
yield (): void Abandons the remainder of this thread's timeslice.

isRunning

isRunning (threadId: string): bool

Parameters:

Returns:

Exceptions:


wait

wait (threadId: string): void

Parameters:

Exceptions:

Notes:


self

self (): string

Returns:


sleep

sleep (msec: int): void

Parameters:

Exceptions:

Notes:


spawn

spawn (threadId: string, arg: `a, f: `a -> void): void

Parameters:

Notes:


yield

yield (): void

Notes:


[ sean.p.miller at gmail dot com ]

[ last updated Saturn's Day, 7 Apr 2012 ]