Examples for function 'de'
Reference doc for 'de'.The function 'de' is of type FSUBR (which is a built-in FEXPR), which can have any number of arguments, that won't be evaluated. When actual parameters are missing during a call, but are required, they will be substituted with a NIL value by default.
Notes
- before each example, PicoLisp is restarted to avoid side effects
- nil and t are just symbols. They happen to be already bound to built-in functions, but you could redefine them :-)
- NIL and T are the boolean values for False and True
- NIL is equal to the empty list () and more (Reference doc for NIL)
Value of symbol 'de'
: de -> 266836returns the address of function 'de' in memory
PicoLisp substitutes missing but required arguments with the value NIL
: (de) !? (de) NIL -- Protected symbolis equivalent to:
: (de NIL NIL) !? (de NIL NIL) NIL -- Protected symbolso, redefining NIL (as well as other protected system objects) is obviously not allowed
First argument of 'de' must be an unprotected symbol
: (de 1) !? (de 1) 1 -- Symbol expectedbecause PicoLisp wants to bind "something" to it.
Function 'de' just sets a symbol to some value (which *can* be a function definition)
: (de foo A B C) -> foo : foo -> (A B C)which is (under the hood) equivalent to
: (de foo . (A B C)) -> foo : foo -> (A B C)which explains why a function can have multiple body expressions
: (de bar (A B) (println A) (println B) (println (+ A B)) (+ 5 6)) -> bar : bar -> ((A B) (println A) (println B) (println (+ A B)) (+ 5 6))where A and B are the formal parameters and we also see 4 expressions in the function body
: (bar 1 2) 1 -> output of first 'println' 2 -> output of second 'println' 3 -> output of third 'println' -> 11 -> result of the expression (+ 5 6)so, indeed you can use other functions to get the same outcome.
The function 'de' isn't really needed (e.g. for creating a function definition)
: (setq baz '((p q) (println p) (println q) (+ p q))) -> ((p q) (println p) (println q) (+ p q)) : baz -> ((p q) (println p) (println q) (+ p q)) : (baz 1 2) 1 2 -> 3References setq
Also a function definition is nothing special, It is just a list
: (de foo (X Y) (println X) (println Y) (+ X Y)) -> foo : foo -> ((X Y) (println X) (println Y) (+ X Y)) : (car foo) -> (X Y) : (cadr foo) -> (println X) : (caddr foo) -> (println Y) : (cadddr foo) -> (+ X Y)data is code and code is data :-)
References car cadr caddr cadddr
Check if what 'de' has generated is a correct function
: (de foo 1 2 3) -> foo : foo -> (1 2 3) : (fun? foo) -> NIL : (de bar (A B) (+ A B)) -> bar : bar -> ((A B) (+ A B)) : (fun? bar) -> (A B)Function 'fun?' returns the lambda expression if it is a valid function, otherwise NIL.
References fun?
Check if what 'de' has generated is a correct function
A final note:
Indeed you can achieve similar results using either 'de' or 'setq'.
However, keep in mind that 'de' is an FEXPR (or for that matter an FSUBR) which does not evaluate its arguments.
: (setq var1 (+ 2 3)) -> 5 : var1 -> 5 # Argument of 'setq' was evaluated : (de foo (+ 2 3)) -> foo : foo # Argument of 'de' was NOT evaluated -> ((+ 2 3))References setq
https://picolisp.com/wiki/?pcedefunction
28may18 | ArievW |