Our derivation of functional programming extensions now arrives at variables and structures used as constructors or functions, and at their combination with non-ground and non-deterministic calls.

*Constructor variables and structures*, introduced in a
relational context (subsection 2.5),
are also useful in a functional setting. For instance, a function
`genints` enumerates the integers in the alternating order
, , , ..., returned as the infinitely non-deterministic values `0` or `s[0]` or `p[0]` or `s[s[0]]` or `p[p[0]]` or ... Its definition employs a constructor variable,
`Sign`, for building up a homogeneous, `absolute' nesting before binding
the structure's ``neutral signs'' (equal in all levels!) to either the
`s`uccessor or `p`redecessor constructor. Instead of using the constants
`s` and `p`, we could also apply as constructors the defined
functions `1+` and `1-`
or structures like
`inc[1]` and `inc[-1]`.

While the maingenints() :-& 0. genints() :-& genints(Sign[0]). genints(Sign[N]) :- Sign is s & Sign[N]. genints(Sign[N]) :- Sign is p & Sign[N]. genints(Sign[N]) :-& genints(Sign[Sign[N]]).

*Function variables in queries* can be utilized much
like the corresponding relation variables
(see subsection 2.5). For example,
given the DATAFUN version of the `density` database
(cf. subsection 3.1.2),
the query `F(china)` asks for all unary properties of `china`,
enumerating the attribute `F` = `area` with the
returned value `3380`, the attribute `F` = `pop`
with its value, etc.

*Function variables in clauses* give us the abstraction power of
functional arguments in the fashion of functional programming.
Thus, `revise` is a ternary function applying any
unary function `F` to
the Nth element of a list (for N greater than the list length
or N less than 1 it returns the list unchanged):

Similarly, therevise(F,N,[]) :-& []. revise(F,1,[H|T]) :-& tup(F(H)|T). revise(F,N,[H|T]) :-& tup(H|revise(F,1-(N),T)).

Note that the free variableattval(Obj,Valfilter) :-& tup(Attribute,Valfilter(Attribute(Obj))). numfilter(X) :- numberp(X) & X.

*Function structures* can be employed like
``function-forming operators'' in FP [Bac78].
Bringing the
relational-product example in subsection 2.5
back to functional programming,
functional composition can be defined by using the
structure `compose[F,G]` as a function,
which permits `twice` to be defined
as a `compose`-structure-valued footed fact:

Again, while the structurecompose[F,G](X) :-& F(G(X)). twice(F) :-& compose[F,F].

Let us now turn to the combination of higher-order operators with non-ground and non-deterministic calls.

For example, , the `inv`ersion of a unary function `F`,
can be defined as
a function structure `inv[F]` which calls `F` freely
within an `is`-call only accepting `F` values that match
the argument `V` of (for an N-ary `F` we just add a ```|`''):

Thus,inv[F](V) :- V is F(X) & X. (generally inv[F](V) :- V is F(|X) & X.)

Another example, a version of the -operator, additionally
employs a result-splitting-like technique
(used in the `sort` definition) to fork the
entire result of a call into both the binding of a
request variable and the returned value.
First, we define a non-deterministic generator `naturals`,
enumerating the naturals from an initialization
given in the first argument, where the next natural is
always both bound to the second argument and returned.

For instance,naturals(N,N) :-& N. naturals(N,V) :-& naturals(1+(N),V).

mu(F) :- 0 is F(naturals(0,V)) & V.

The `ap` reduction of higher-order relations in
subsection 2.5
directly transfers to function variables and structures.
For instance, the above rule could be reduced to

changingap(mu,F) :- 0 is ap(F,ap(naturals,0,V)) & V.