-
Notifications
You must be signed in to change notification settings - Fork 12
5. Description of Functions, Operators and Libraries
- 5.1 Operators
- 5.2 Functions
- 5.3 A la APL
- 5.4 A la Haskell
- 5.5 Math and Random Functions
- 5.6 System And Dates
- 5.7 Files
- 5.8 Strings
- 5.9 Regular Expressions
- 5.10 Graphic Instructions
- 5.11 Ontologies
- 5.12 Sockets
- 5.13 XML
- 5.14 SQLite Library
- 5.15 Transducer Library
- Index of Functions
- Index of Haskell Functions
- Index of APL Functions
A few notes on the particularities of LispE regarding its formalism:
LispE manipulates strings as expressions between two "...".
The '\' is used as escape character. LispE also recognizes expressions: \n, \r and \t
(setq s "This and that and there")
You can also use the character accent grave (or backquote): `` to identify long strings containing all kinds of characters:
(setq s `This is one " and one " in the same string`)
When you have a long list of parentheses that closes an expression, you can use ]
to generate the right amount of closing parentheses...
; this will automatically generate all the missing parentheses
(setq x (cons 'e '(]
LispE accepts dictionary definitions with braces (à la Python). Keys and values must be separated by a ":". There are three types of dictionaries:
- Indexed on strings (implemented as a std::map, keys are sorted out)
- Indexed on integers (implemented as a std::unordered_map, keys are NOT sorted out)
- Indexed on numbers (implemented as a std::unordered_map, keys are NOT sorted out)
Depending on the description provided, LispE can create either one or the others. More precisely, if all the keys are numeric, it will be a dictionary indexed on numbers, conversely if at least one of the keys is a string, it will be a dictionary indexed on strings. If you are not sure that your description leads to the right type of dictionary, it is better to use key, keyi or keyn to create your dictionaries.
Important: this way of declaring a dictionary leads to the construction of a set of key instructions that will be evaluated to generate a dictionary later on. It is then possible to add variables in this description, since the evaluation will be take later.
; Numerical dictionary
(setq d {12:45 45:67 90:20})
; Alphabetical dictionary, numeric keys become strings
; If a key is anything but a number, then the whole definition
; becomes an alphabetical dictionary
(setq d {"a": 2 "b":10 12:45 45:67 90:20 "k":9})
Note that the key:value are separated from each other by a space .
The following formalism: @{k:v..}
is also used to create a dictionary. However, the difference with the description above, is that in this case a dictionary is directly constructed, while the other formalism compiles the expression into a set of instructions.
Important Since the evaluation is done at compile time, you cannot have variables in this expression...
Furthermore, a data dictionary is by definition indexed on strings.
; This expression is directly compiled into a dictionary
(setq d @{"a": 2 "b":10 12:45 45:67 90:20 "k":9})
LispE also provides sets, which can either contains strings, integers or numbers (sets, seti, setn). It also provides set to create sets of objects.
A set can also be created with: {...}.
(setq st {1 2 3 4})
(in st 2) ; true
(in st 100) ; nil
LispE provides a heap structure, which can be used to store elements in specific order defined by a operator or a lambda expression.
(see heap for more information)
LispE offers some specific types to handle lists of values, instead of lists of objects, which means that values can be stored and removed without any kind of reference counting management.
Each of these methods also corresponds to a function that will transform a regular list into a list of values.
These objects are returned by methods such as range or iota but also by many string methods.
(iota 10.0) ; yields the 'list of numbers': (1 2 3 4 5 6 7 8 9 10)
(numbers '(1 2 3 4)) ; transforms this list containing numbers into a list of numbers.
Matrix is another type, which is provided to handle matrices as a list of lists. However, you need to provide its size in rows and columns.
- (matrix r c initial): this will create a matrix with r rows and c columns, initialised with the value initial.
Note that initial is optional. The default value is 0.
(matrix 2 3 1) ; yields ((1 1 1) (1 1 1))
(matrix 3 5) ; yields ((0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0))
A matrix object can also be created with a rho instruction.
(rho 3 3 (integers 1 3 4)) ; yields ((1 3 4) (1 3 4) (1 3 4))
Note that rho can also returns the matrix dimension:
(rho (matrix 5 7)) ; yields (5 7)
To access an element of a matrix, you can use the operator at with two indexes.
; we create a matrix with rho
(setq m (rho 4 5 (integers 1 4 9 10 8))) ; m is ((1 4 9 10 8) (1 4 9 10 8) (1 4 9 10 8) (1 4 9 10 8))
; we display the element at row: 2, column 3
(println (at m 2 3)) ; yields 10
; We modify it
(at m 2 3 100) ; m is now ((1 4 9 10 8) (1 4 9 10 8) (1 4 9 100 8) (1 4 9 10 8))
A tensor object is a multidimensional matrix beyond NxM. A tensor can either be created with all values set to 0, or through a rho instruction. Note that the outer-product can also create a tensor objects.
(tensor 2 3 3) ; yields (((0 0 0) (0 0 0) (0 0 0)) ((0 0 0) (0 0 0) (0 0 0))
(rho 2 3 3 (iota 18)) ; yields (((1 2 3) (4 5 6) (7 8 9)) ((10 11 12) (13 14 15) (16 17 18)))
When defining the parameters of a function, via defun or lambda, it is possible to declare certain parameters as optional.
To do this, simply declare the parameters as a list. If these lists contain two items, then the second one is considered as the default value.
(defun test (i (j -2)) (+ i j))
(print test(10 20))
30
(print test(10))
8
LispE provides a specific notation for functions that can take a variable number of arguments. This notation is also a list where the first element is the empty list and the second is a parameter in which supernumerary arguments are stored as in a list. This description should be the last element of your parameter definition.
; All arguments after 'x' are stored in 'l'
(defun variadic(x (() l)) ...)
; this function takes at least two arguments
(defun test(x y (() l))
(println x y l)
)
(test 10 20 30 45 90 900 10) ; displays: 10 20 (30 45 90 900 10)
Important: These list items should always be declared at the end of the parameter list, not in the middle.
This variable contains the list of values passed as arguments on the command line.
This variable contains the path name of the current file being executed.
These values are predefined with:
_pi = 3.14159
_tau = 2 * _pi
_e = 2.71828
_phi = 1.61803
Description of the different operators in operators
Note that LispE allows for an infix notation of mathematical expressions: see Infix
Here is the list of functions available in lispe:
@ : get value from a container at positions k1..kn
check : (check CONDITION I1..In)
cond: (cond (cond1 instructions)...)
containerkeys : (containerkeys dict)
containervalues : (containervalues dict)
deflib: Internal use of the interpreter
defpat: pattern function definition
droplist: (droplist condition list)
extract: (extract e i1 (i2)), i2 is optional
fappend: (append pathname data)
filterlist: (filterlist condition list)
findall : (findall chaine chaine pos)
fwrite: (fwrite pathname data)
heap: (heap comparison v1 v2...)
if : (if (predicate) THEN ELSE)
lambda : lambda function definition
loop : (loop var liste instruction1 instruction2...)
loopcount: (loopcount nb instructions)
lloop (x1 x2 x3...) l1 l2 l3... instructions)
mloop : (mloop (x1 x2 x3...) c1 c2 c3... instructions)
maplist : (maplist function list)
mark: (mark l B) marks lists for controlling infinite loops
max : (max a1 a2 a3) / (max '(e1 e2 e3))
min : (min a1 a2 a3) / (min '(e1 e2 e3))
minmax : (minmax a1 a2 a3) / (minmax '(e1 e2 e3))
ncheck: (ncheck CONDITION ELSE I1 I2..)
nullp : check if the argument is nil
numberp : check if the argument is a number
println : (println e1 e2 .. en)
printerr : (printerr e1 e2 .. en)
printerrln : (printerrln e1 e2 .. en)
range : (range initial limite pas)
replaceall : (replaceall object search replace)
resetmark: resets marks for controlling infinite loops
setrange : modify container between position b e
set@ : modify container at positions k1...kn
takelist: (takelist condition list)
threadclear : clear protected list for threads
threadretrieve : protected list for threads
threadstore : (threadstore namespace e)
wait : wait for threads to end
while : (while (condition) instruction1 instruction2 ...)
zerop : returns true if value is zero
filter : (filter condition list)
dropwhile : (dropwhile condition list)
replicate : (replicate nb value)
takewhile : (takewhile condition liste)
irange: (irange initial increment)
foldl : (foldl op list initial)
foldr : (foldr op list initial)
scanl : (scanl op liste initial)
scanr : (scanr op list initial)
zipwith : (zipwith op l1 l2 l3...)
Description of these operators in APL