diff --git a/doc/Doc.md b/doc/Doc.md index 55b0210..0fc27b6 100644 --- a/doc/Doc.md +++ b/doc/Doc.md @@ -21,146 +21,145 @@ ## 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.| -|`(cond (test1 action1) (test2 action2) ... (testn actionn))`|The first clause whose test evaluates to non-nil is selected; all other clauses are ignored, and the consequents of the selected clause are evaluated in order. If none of the test conditions are evaluated to be true, then the cond statement returns nil.|This special form is 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.| -|`(and ...)`|`and` evaluates logical and on its list argument.|`and` evaluates only until its true, it stops when becomes false. -|`(or ...)`|`or` evaluates logical or on its list argument.|`or` evaluates while its false, it stops when becomes true.| -|`(benchmark msg_string ...)`|`benchmark` 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 and prints msg_string and timing info.|| +|Special Form|Argument Evaluations|Purpose|Section| +|:-|-|-|-| +|`(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.|Language| +|`(cond (test1 action1) (test2 action2) ... (testn actionn))`|The first clause whose test evaluates to non-nil is selected; all other clauses are ignored, and the consequents of the selected clause are evaluated in order. If none of the test conditions are evaluated to be true, then the cond statement returns nil.|This special form is method of control flow.|Language| +|`(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.|Language| +|`(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.|Language| +|`(defun name params body)`|`defun` evaluates none of its arguments.|This special form allows the user to conveniently define functions.|Language| +|`(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.|Language| +|`(lambda params body)`|`lambda` evaluates none of its arguments.|This special form allows the user to define anonymous functions.|Language| +|`(quote x)`|`quote` evaluates none of its arguments.|This is equivalent to the `'expr` syntactic sugar.|Language| +|`(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.|Language| +|`(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.|Language| +|`(and ...)`|`and` evaluates logical and on its list argument.|`and` evaluates only until its true, it stops when becomes false.|Language| +|`(or ...)`|`or` evaluates logical or on its list argument.|`or` evaluates while its false, it stops when becomes true.|Language| +|`(benchmark msg_string ...)`|`benchmark` 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 and prints msg_string and timing info.||Language| ## Library -|Signature|Description|Return values| -|:-|-|-| -|`(= a b)`|Test whether two values are equal|`1` when equal otherwise `nil`| -|`(!= a b)`|Test whether two values are not equal|`1` when not equal otherwise `nil`| -|`(> a b)`|Test whether one value is greater to another |`1` when greater otherwise `nil`| -|`(< a b)`|Test whether one value is less to another |`1` when less otherwise `nil`| -|`(>= a b)`|Test whether one value is greater or equal to another |`1` when greater or equal otherwise `nil`| -|`(<= a b)`|Test whether one value is less or equal to another |`1` when less or equal otherwise `nil`| -|`(+ 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. First element is at index 0|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 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|| -|`(read-file-lines filename lambda)`|Reads file and for each line call lambda with passing the line as a parameter|`(read-file-lines "/tmp/f.txt" (lambda (ln) (print ln))`| -|`(write-file filename content-str)`|Write a string to a file|| -|`(read-url url [headers] [body] [method])`|Reads URL|Returns list (status-code content)| -|`(system-cmd command_str)`|Execute system command|| +|Signature|Description|Return values|Section| +|:-|-|-|-| +|`(= a b)`|Test whether two values are equal|`1` when equal otherwise `nil`|Language| +|`(!= a b)`|Test whether two values are not equal|`1` when not equal otherwise `nil`|Language| +|`(> a b)`|Test whether one value is greater to another |`1` when greater otherwise `nil`|Language| +|`(< a b)`|Test whether one value is less to another |`1` when less otherwise `nil`|Language| +|`(>= a b)`|Test whether one value is greater or equal to another |`1` when greater or equal otherwise `nil`|Language| +|`(<= a b)`|Test whether one value is less or equal to another |`1` when less or equal otherwise `nil`|Language| +|`(+ a b ..)`|Sum multiple values |sum of values|Language| +|`(- a b)`|Substract two values |result of substraction|Language| +|`(* a b ..)`|Multiply several values|result of multiplication|Language| +|`(/ a b)`|Divide two values|result of division|Language| +|`(% a b)`|Remainder of division|result of operation|Language| +|`(list ..)`|Create a list of values||List manipulation| +|`(insert list index element)`|Insert an element into a list. Indexed from 0|new list with value inserted|List manipulation| +|`(index list index)`|Return element at index in list. First element is at index 0|Element at index|List manipulation| +|`(remove list index)`|Remove a value at an index from a list|List with element removed|List manipulation| +|`(len list)`|Get the length of a list|list length|List manipulation| +|`(push list element)`|Add an item to the end of a list|new list with element added|List manipulation| +|`(pop list)`|Returns last element of list|Last element|List manipulation| +|`(head list)`|Returns first element of a list|First element|List manipulation| +|`(tail list)`|Return all elements of list except first one|List without first element|List manipulation| +|`(first list)`|Returns first element of a list|First element|List manipulation| +|`(last list)`|Returns last element of list|Last element|List manipulation| +|`(range low high)`|Returns list with elements in range low .. high|`(range 1 5) => (1 2 3 4)`|List manipulation| +|`(member list item)`|Returns true when list contains item||List manipulation| +|`(uniq list)`|Returns list with removed duplicates ||List manipulation| +|`(make-list len)`|Return list with len nil elements|`(make-list 3) => (nil nil nil)`|List manipulation| +|`(make-list-of len value)`|Return list with len value elements||List manipulation| +|`(map ..)`||`(map (lambda (x) (+ x 10)) '(1 2 3 4 5 6)) => (11 12 13 14 15 16)`|List manipulation| +|`(filter lambda list)`||`(filter (lambda (x) (> x 2)) '(1 2 3 4 5)) => (3 4 5)`|List manipulation| +|`(reduce lambda acumulator list)`||`>>> (reduce (lambda (x y) (+ (* x 10) y)) 0 '(1 2 3 4)) => 1234`|List manipulation| +|`(exit code)`|Exit the program with an integer code||System| +|`(quit code)`|Same as (exit ..)||System| +|`(print ..)`|Print several values and return the last one||IO| +|`(input [prompt])`|Get user input with an optional prompt||IO| +|`(random low high)`|Get a random number between two numbers inclusively||System| +|`(include file)`|Read a file and execute its code||IO| +|`(read-file filename)`|Get the contents of a file||IO| +|`(read-file-lines filename lambda)`|Reads file and for each line call lambda with passing the line as a parameter|`(read-file-lines "/tmp/f.txt" (lambda (ln) (print ln))`|IO| +|`(write-file filename content-str)`|Write a string to a file||IO| +|`(read-url url [headers] [body] [method])`|Reads URL|Returns list (status-code content)|IO| +|`(system-cmd command_str)`|Execute system command||System| |`(ls-dir dir)`|List a dir|List of directory entries| -|`(is-file? filename)`|Returns true if passed filename is a file|| -|`(is-dir? filename)`|Returns true if passed filename is a directory|| -|`(tcp-server port handler)`|Starts listening on port and when request comes calls passed lambda and writes its returned value back to client. Lambda must return either string or two element list where first element is boolean and second string.When first element is true it closes listening socker.|(`tcp-server 7777 (lambda (str) (list #t (string-upcase str))))`| -|`(tcp-client address port data)`|Opens connection to server on port, writes there data and returns response.|`(print (tcp-client "127.0.0.1" 7777 "abcd"))`| -|`(is-dir? filename)`|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|| -|`(get-localtime-offset)`|Offset in seconds between local time and gmt time|| -|`(date-to-str date format)`|Converts date to formated string. Format is strftime format (https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm)|`>>> (date-to-str (get-universal-time) "%Y-%m-%d %H:%M:%S") - => "2021-03-13 19:53:01"`| -|`(str-to-date string format)`|Converst string to time of secs since epoch. |`>>> (str-to-date "2021-03-13 19:53:01" "%Y-%m-%d %H:%M:%S") => 1615665181`| -|`(date-add date amount unit)`|Add number of units to date. A unit is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'|`>>> (date-to-str (date-add (str-to-date "2021-03-13 19:53:01" "%Y-%m-%d %H:%M:%S") 10 "day") "%Y-%m-%d %H:%M:%S") => "2021-03-23 20:53:01"`| -|`start-of-day datetime`||`>>> (start-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1620864000`| -|`end-of-day datetime`||`>>> (end-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1620950399`| -|`start-of-month datetime`||`>>> (start-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1619827200`| -|`start-of-next-month datetime`||`>>> (end-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1622505599`| -|`end-of-next-month datetime`||`>>> (start-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1622505600`| -|`end-of-month datetime`||`>>> (end-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1625097599`| -|`start-of-prev-month datetime`||`>>> (start-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1617235200`| -|`end-of-prev-month datetime`||`>>> (end-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1619827199`| -|`start-of-year datetime`||`>>> (start-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1609459200`| -|`end-of-year datetime`||`>>> (end-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1640995199`| -|`(debug ..)`||| -|`(display ..)`|Displays passed parameters|`>>> (display '(1 2 3)) => "(1 2 3)"`| -|`(string-replace source substr replacement)`|Replace a substring with a replacement string in a source string|`>>> (string-replace "abcdefg" "de" "DE") => "abcDEfg"`| -|`(string-replace-re source substr replacement)`|Replace a substring regex with a replacement string in a source string|`>>> (string-replace-re "there is a subsequence in the string" "\\b(sub)([^ ]*)" "sub-$2") => "there is a sub-sequence in the string"`| -|`(string-regex? where regex)`| Returns true if where contains regex|`>>> (string-regex? "aba123cdefg" "[0-9]+") => 1`| -|`(string-regex-list where regex [mode] [ignorecase])`| Returns list of substring from where captured by regex in mode match or tokens|`>>> (string-regex-list "