Generated by examples/gen-corelib-doc.lisp
.
Defines a variable.
> (def x (+ 1 2))
> x
;=> 3
Defines a function.
> (defun incr (x) (+ x 1))
> (incr 3)
;=> 4
> (defun sum (xs) (if xs (+ (car xs) (sum (cdr xs)) 0)))
> (sum '(1 2 3))
;=> 6
Binds values to local variables.
> (let ((a 1)
(b 2))
(+ a b))
;=> 3
Similar to let
, but when binding values to variables,
their expressions can access the local variables that are being defined.
> (letrec ((f (lambda (n) (if (eqv? n 0) 0 (+ n (g (- n 1))))))
(g (lambda (n) (if (eqv? n 0) 0 (+ n (f (- n 1)))))))
(f 3))
;=> 6
Branches the execution of a code based on a given condition.
> (if t 1 2)
;=> 1
> (if nil 1 2)
;=> 2
> (if (eqv? 1 2)
(error "unreachable"))
;=> nil
Similar to if
, but takes multiple clauses.
> (let ((a 3))
(cond
((eqv? a 2) (echo "foo") "a is 2")
((eqv? a 3) (echo "bar") "a is 3")
(else "not sure")))
bar
;=> "a is 3"
Evaluates a sequence of expressions.
> (begin
(echo "Hello")
123)
Hello
;=> 123
Updates the value of a variable.
> (let ((a 1)
(b 2))
(set! a 999)
(cons a b))
;=> (999 . 2)
Makes an anonymous function.
> ((lambda (a b)
(echo "Calculating...")
(+ a b))
1 2)
Calculating...
;=> 3
Returns an unevaluated argument.
> (quote 3)
;=> 3
> (quote foo)
;=> foo
> (quote (a b c))
;=> (a b c)
> '(a b c) ; shorthand for quote
;=> (a b c)
Makes a cons-cell.
> (cons 1 2)
;=> (1 . 2)
> (cons 'a (cons 'b (cons 'c nil)))
;=> (a b c)
Returns the first element of a cons-cell.
> (car (cons 1 2))
;=> 1
Returns the second element of a cons-cell.
> (cdr (cons 1 2))
;=> 2
Updates the first element of a cons-cell.
> (let ((pair (cons 1 2)))
(set-car! pair 999)
pair)
;=> (999 . 2)
Updates the second element of a cons-cell.
> (let ((pair (cons 1 2)))
(set-cdr! pair 999)
pair)
;=> (1 . 999)
Returns t
if the arguments are structually equal; otherwise returns nil
.
> (equal? 1 1)
;=> t
> (equal? 1 2)
;=> nil
> (equal? "foo" 'foo)
;=> nil
> (equal? (cons 1 2) (cons 1 2))
;=> t
> (let ((pair (cons 1 2)))
(equal? pair pair))
;=> t
Returns t
if the arguments are the same; otherwise returns nil
.
> (eqv? 1 1)
;=> t
> (eqv? 1 2)
;=> nil
> (eqv? "foo" 'foo)
;=> nil
> (eqv? (cons 1 2) (cons 1 2))
;=> nil
> (let ((pair (cons 1 2)))
(eqv? pair pair))
;=> t
Returns t
if an argument is a num; otherwise returns nil
.
> (num? 3)
;=> t
> (num? "foo")
;=> nil
Returns t
if an argument is a text; otherwise returns nil
.
> (text? "foo")
;=> t
> (text? 'bar)
;=> nil
Returns t
if an argument is a symbol; otherwise returns nil
.
> (symbol? 'bar)
;=> t
> (symbol? t)
;=> nil
Returns t
if an argument is t
; otherwise returns nil
.
> (t? t)
;=> t
> (t? nil)
;=> nil
Returns t
if an argument is nil
; otherwise returns nil
.
> (nil? nil)
;=> t
> (nil? (cons 1 2))
;=> nil
Returns t
if an argument is a cons-cell; otherwise returns nil
.
> (cons? (cons 1 2))
;=> t
> (cons? 3)
;=> nil
Adds arguments.
> (+)
;=> 0
> (+ 1)
;=> 1
> (+ 1 2 3)
;=> 6
Subtracts arguments.
> (- 3)
;=> -3
> (- 3 2)
;=> 1
> (- 3 2 1)
;=> 0
Multiplies arguments.
> (*)
;=> 1
> (* 3)
;=> 3
> (* 3 4 -5)
;=> -64
Divides arguments.
> (/ 3)
;=> .33333333333333333333
> (/ 9 3)
;=> 3
> (/ 9 3 3)
;=> 1
> (/ 9 0)
Error: zero_division: division by zero
at native function <div_impl__959_v0>
Returns the remainder of a division.
> (% 7 3)
1
> (% -7 3)
-2
> (% 7 0)
Error: zero_division: division by zero
at native function <mod_impl__959_v0>
Makes a list.
> (list)
;=> nil
> (list 1 2 3)
;=> (1 2 3)
Applies a function to each element of a list.
> (defun double (x) (* x 2))
> (map double nil)
;=> nil
> (map double (list 1 2 3))
;=> (2 4 6)
Returns a sequence of numbers.
> (iota 3)
;=> (0 1 2)
> (iota 1 5)
;=> (1 2 3 4)
> (iota 0)
;=> nil
Raises a user error.
> (error "wrong argument!")
Error: user_error: wrong argument!
at native function <error_impl__677_v0>
> (error "my_error" "something is wrong") ; custom error code
Error: my_error: something is wrong
at native function <error_impl__677_v0>
Catches an error.
> (catch ("zero_division"
(echo "Do not divide by zero!")
'fail)
(echo (object->text (/ 10 0)))
'ok)
Do not divide by zero!
;=> fail
> (catch ("some_error" 'handled)
(error "another_error" "This error will not be caught"))
Error: another_error: This error will not be caught
at native function <error_impl__677_v0>
Prints arguments.
> (echo "Hello")
Hello
;=> nil
> (echo "Hello" "world")
Hello
world
;=> nil
Converts an object into a text.
> (object->text 123)
;=> "123"
> (object->text '(1 a t nil))
;=> "(1 a t nil)"
Evaluates an expression.
> (eval '(+ 1 2))
;=> 3
> (let ((a 1) (b 2)) (eval '(cons a b)))
;=> (1 . 2)
Exits the interpreter.
> (exit)
;=> interpreter exits
Shows this help.
> (help)
## Function: `+`
Adds arguments.
...
## Function: `-`
Subtracts arguments.
...
;=> nil