mlisp/doc/Doc.md

83 lines
5.3 KiB
Markdown

## Syntax and Special Forms
|Special Form|Argument Evaluations|Purpose|
|:-|-|-|
|`(if cond a b)`|`if` only evaluates its `cond` argument. If `cond` is truthy (non-zero), then `a` is evaluated. Otherwise, `b` is evaluated.|This special form is the main method of control flow.|
|`(do a b c ...)`|`do` takes a list of s-expressions and evaluates them in the order they were given (in the current scope), and then returns the result of the last s-expression.|This special form allows lambda functions to have multi-step bodies.|
|`(scope a b c ...)`|`scope` takes a list of s-expressions and evaluates them in the order they were given _in a new scope_, and then returns the result of the last s-expression.|This special form allows the user to evaluate blocks of code in new scopes.|
|`(defun name params body)`|`defun` evaluates none of its arguments.|This special form allows the user to conveniently define functions.|
|`(define name value)`|`define` evaluates the `value` argument, which is then assigned to `name` in the current scope.|This special form allows the user to bind atoms to values in a scope.|
|`(lambda params body)`|`lambda` evaluates none of its arguments.|This special form allows the user to define anonymous functions.|
|`(quote x)`|`quote` evaluates none of its arguments.|This is equivalent to the `'expr` syntactic sugar.|
|`(for x list ...)`|`for` evaluates only its list argument.|`for` iterates through the list storing each element in `x`, and then evaluating all of the rest of the values in the `for` body. It then returns the last value evaluated.|
|`(while cond ...)`|`while` evaluates only its cond argument.|`while` evaluates its condition expression every iteration before running. If it is true, it continues to evaluate every expression in the `while` body. It then returns the last value evaluated.|
## Library
|Signature|Description|Return values|
|:-|-|-|
|`(= a b)`|Test whether two values are equal|`1` when equal otherwise `0`|
|`(!= a b)`|Test whether two values are not equal|`1` when not equal otherwise `0`|
|`(> a b)`|Test whether one value is greater to another |`1` when greater otherwise `0`|
|`(< a b)`|Test whether one value is less to another |`1` when less otherwise `0`|
|`(>= a b)`|Test whether one value is greater or equal to another |`1` when greater or equal otherwise `0`|
|`(<= a b)`|Test whether one value is less or equal to another |`1` when less or equal otherwise `0`|
|`(+ a b ..)`|Sum multiple values |sum of values|
|`(- a b)`|Substract two values |result of substraction|
|`(* a b ..)`|Multiply several values|result of multiplication|
|`(/ a b)`|Divide two values|result of division|
|`(% a b)`|Remainder of division|result of operation|
|`(list ..)`|Create a list of values||
|`(insert list index element)`|Insert an element into a list. Indexed from 0|new list with value inserted|
|`(index list index)`|Return element at index in list|Element at index|
|`(remove list index)`|Remove a value at an index from a list|List with element removed|
|`(len list)`|Get the length of a list|list length|
|`(push list element)`|Add an item to the end of a list|new list with element added|
|`(pop list)`|Returns last element of list|Last element|
|`(head list)`|Returns first element of a list|First element|
|`(tail list)`|Return all elements of list except first one|List without first element|
|`(first list)`|Returns first element of a list|First element|
|`(last list)`|Returns last element of list|Last element|
|`(range low high)`|Returns list with elements in range low .. high|`(range 1 5) => (1 2 3 4)`|
|`(member list item)`|Returns true (1) when list contains item||
|`(uniq list)`|Returns list with removed duplicates ||
|`(make-list len)`|Return list with len nil elements|`(make-list 3) => (nil nil nil)`|
|`(make-list-of len value)`|Return list with len value elements||
|`(map ..)`||`(map (lambda (x) (+ x 10)) '(1 2 3 4 5 6)) => (11 12 13 14 15 16)`|
|`(filter lambda list)`||`(filter (lambda (x) (> x 2)) '(1 2 3 4 5)) => (3 4 5)`|
|`(reduce lambda acumulator list)`||`>>> (reduce (lambda (x y) (+ (* x 10) y)) 0 '(1 2 3 4)) => 1234`|
|`(exit code)`|Exit the program with an integer code||
|`(quit code)`|Same as (exit ..)||
|`(print ..)`|Print several values and return the last one||
|`(input [prompt])`|Get user input with an optional prompt||
|`(random low high)`|Get a random number between two numbers inclusively||
|`(include file)`|Read a file and execute its code||
|`(read-file filename)`|Get the contents of a file||
|`(write-file filename)`|Write a string to a file||
|`(read-url url [headers])`|Reads URL|Returnd list (status_code content)|
|`(system-cmd command_str)`|Execute system command||
|`(ls-dir ..)`|List a dir|List of directory entries|
|`(is-file? ..)`|Returns true if passed filename is a file||
|`(is-dir? ..)`|Returns true if passed filename is a directory||
|`(parse-csv string)`|Parse CSV string||
|`(parse-json json_string)`|Parse JSON string||
|`(save-csv ..)`|||
|`(get-universal-time)`|Get current time as secs from epoch||
|`(date-to-str ..)`|||
|`(str-to-date ..)`|||
|`(date-add ..)`|||
|`(debug ..)`|||
|`(display ..)`|||
|`(replace ..)`|||
|`(regex-search? ..)`|||
|`(string-pad str len char rpad_lpad)`|||
|`(int ..)`|||
|`(float ..)`|||
|`(eval ..)`|||
|`(type ..)`|||
|`(parse ..)`|||
|`(make-list size)`|||
|`(make-list-of size value)`|||
|`(xx ..)`|||