Node: Functions, Next: , Previous: Subtypes, Up: Top



Functions

As with Common Lisp Lpp has first class function objects. That is to say that objects can be created that represent C++ functions and manipulated like any other Lpp object. Such function objects are easily created with the L conversion macro. For example

     let someFun(let x, let y) {...}
     let functionList = list(L(car), L(someFun))
     

In this example functionList contains a list of two function objects: car an existing Lpp function and some other function object called someFun. In general any function object can be generated from any C++ function with an address (non-inline) of type let with 0 or more let type arguments. Note that Lpp functions that return ints can not be used but they have counterparts that can. For example length can not but lengthL can.

Such function objects can be called using either funcall or apply, See Control Structure. For example if in the previous example someFun given two arguments returns some list then

     let test(let a, let b, let functionList) {
       return funcall(nth(0, functionList),
                      funcall(nth(1, functionList), a, b));}
     

would return the car of that list. Actually notice that test in this example could be used to apply any Lpp function of one argument to any Lpp object returned by any function of two arguments. Using first class function objects as variables adds an important dimension to programming.

ARGSn Macro

Where n is the number of function arguments. For example: ARGS0, ARGS1, ARGS2 ... etc. This macro makes it easy to cast function objects of functions that can have more than one argument such as list. For example:

     let fun = L(ARGS3 list);
     print(fun) => <Lpp Function of 3 args>
     funcall(fun, S(a), S(b), S(c)) => (a b c)
     

Lpp only supplies up to 10 arguments for such functions but a quick look at the Lpp include file LppPure.hh makes it apparent that any number of arguments can be added easily. However, very rarely are more than 10 arguments needed and if you are habitually using more than 10 arguments then you are probably doing something wrong or else you should be using something like listEM or listSEM, See List operations.