lifp

☯️ A Lisp dialect and toolchain. All in one binary.

View the Project on GitHub shikaan/lifp

lifp - v0.3.0 (787dadf)

Table of Contents

specials

Special forms for the lifp language. These are core language constructs that control binding, scope, and conditional execution.

cond

Conditional branching. Evaluates the first true condition's form, or the last form if none match.

(cond ((< x 0) "negative") ((= x 0) "zero") ("positive"))

def!

Binds a value to a symbol in the current environment.

(def! answer 42)
(def! sum (fn (a b) (+ a b)))

fn

Creates a function (closure) with given arguments and body.

(fn (a b) (+ a b))

let

Binds local variables for the scope of a body expression.

(let ((x 1) (y 2)) (+ x y)) ; returns 3

core

Core lifp operators. For all intents and purposes, these should be thought as language keywords.

(and true false) ; returns false

%

Performs a modulo division.

(% 6 3) ; returns 0

*

Multiplies arguments.

(* 1 2 3) ; returns 6

+

Sums arguments.

(+ 1 2 3) ; returns 6

-

Subtracts arguments from the first argument.

(- 6 3 2) ; returns 1

/

Divides the first argument by the rest.

(/ 6 3 2) ; returns 1

<

Checks if the first argument is less than the second. otherwise.

(< 1 6) ; returns true

<=

Checks if the first argument is less than or equal to the second. second, false otherwise.

(<= 1 6) ; returns true

<>

Checks if two arguments are not equal.

(<> 6 6) ; returns false

=

Checks if two arguments are equal.

(= 6 6) ; returns true

>

Checks if the first argument is greater than the second. false otherwise.

(> 1 6) ; returns false

>=

Checks if the first argument is greater than or equal to the second. second, false otherwise.

(>= 6 1) ; returns true

and

Logical AND operation on boolean arguments.

(and true false) ; returns false

or

Logical OR operation on boolean arguments.

(or true false) ; returns true

flow

Flow control utilities for lifp. These functions provide basic control over program execution, such as pausing execution for a specified duration.

(flow:sleep! 1000) ; pauses execution for ~1 second

flow:sleep!

Suspends execution for a given number of milliseconds.

(flow:sleep! 1000) ; pauses for ~1 second

io

Input/output utilities for lifp. These functions provide basic console IO.

(io:stdout! "hello") ; prints to stdout
(io:stderr! "error") ; prints to stderr
(io:printf! "Hello, {}!" ["world"]) ; prints formatted string
(io:readline! "Enter your name: ") ; reads a line from stdin
(io:clear!) ; clears the terminal

io:clear!

Clears the terminal screen.

(io:clear!)

io:printf!

Prints a formatted string to standard output, replacing each '{}' in the format string with the corresponding value from the list.

(io:printf! "Hello, {}!" ("world")) ; prints "Hello, world!"

io:readline!

Prints a prompt and returns the answer as a string.

(io:readline! "What's your name?") ; returns user input

io:stderr!

Prints a value to standard error.

(io:stderr! "error")

io:stdout!

Prints an argument to standard output.

(io:stdout! "hello")

list

List manipulation operators for lifp. These functions provide core list operations such as counting, creating, accessing, mapping, filtering, and iterating over lists.

(list:count (list:from 1 2 3)) ; returns 3
(list:nth 1 (list:from 10 20 30)) ; returns 20
(list:map (fn (x i) (* x 2)) (list:from 1 2 3)) ; returns (2 4 6)

list:count

Counts the number of elements in a list.

(list:count (1 2 3)) ; returns 3

list:each

Applies a function to each element of a list for side effects. The function receives each element and its index.

(list:each (fn (x i) (print x)) (1 2 3))

list:filter

Filters a list using a predicate function. The function receives each element and its index, and should return a boolean. true.

(list:filter (fn (x i) (> x 1)) (1 2 3)) ; returns (2 3)

list:from

Creates a list from the given arguments.

(list:from 1 2 3) ; returns (1 2 3)

list:map

Maps a function over a list, returning a new list of results. The function receives each element and its index.

(list:map (fn (x i) (* x 2)) (1 2 3)) ; returns (2 4 6)

list:nth

Returns the nth element of a list, or nil if out of bounds.

(list:nth 1 (10 20 30)) ; returns 20

list:reduce

Reduces a list to a single value by repeatedly applying a reducer function. The function receives the accumulated value, the current element, and its index.

(list:reduce (fn (p c i) (+ p c)) 0 (1 2 3)) ; returns 6

list:times

Calls a function a given number of times, collecting the results in a list. The function receives the current index.

(list:times (fn (i) (* i 2)) 3) ; returns (0 2 4)

math

Math operators for lifp. Provides mathematical utilities such as min, max, random, ceil, and floor for working with numbers and lists of numbers.

(math:max (list:from 1 2 3)) ; returns 3
(math:min (list:from 1 2 3)) ; returns 1
(math:random!) ; returns a random number between 0 and 1
(math:ceil 2.3) ; returns 3
(math:floor 2.7) ; returns 2

math:ceil

Returns the smallest integer greater than or equal to the argument.

(math:ceil 2.3) ; returns 3

math:floor

Returns the largest integer less than or equal to the argument.

(math:floor 2.7) ; returns 2

math:max

Returns the maximum of the arguments.

(math:max 1 2 3) ; returns 3

math:min

Returns the minimum of the arguments.

(math:min 1 2 3) ; returns 1

math:random!

Returns a random number between 0 and 1.

(math:random!) ; returns a random number between 0 and 1

str

String manipulation functions for lifp.

(str:length "hello") ; returns 5
(str:join "," ("a" "b" "c")) ; returns "a,b,c"
(str:slice "abcdef" 1 4) ; returns "bcde"
(str:include "hello world" "world") ; returns true
(str:trimLeft "   foo") ; returns "foo"
(str:trimRight "foo   ") ; returns "foo"

str:include?

Checks if a string contains a substring.

(str:include? "hello world" "world") ; returns true

str:join

Joins a list of strings using a separator.

(str:join "," ("a" "b" "c")) ; returns "a,b,c"

str:length

Returns the length of a string.

(str:length "hello") ; returns 5

str:slice

Returns a substring from start to end (end not inclusive). Negative indices count from the end of the string.

(str:slice "abcdef" 1 4) ; returns "bcde"
(str:slice "abcdef" 2) ; returns "cdef"

str:trimLeft

Removes whitespace from the start of a string.

(str:trimLeft "   foo") ; returns "foo"

str:trimRight

Removes whitespace from the end of a string.

(str:trimRight "foo   ") ; returns "foo"