I am trying to implement a simple functional language for automatic program synthesis.
The data structure is a graph of functions and values, which compiles down to javascript.
The following graph should be a fold function. funcApp nodes are connected to a function node and a number of value nodes, and it applies the function to the values. arg0 is the list, arg1 is an initial value (z) arg2 is the function to be applied.

It is equivalent to the folloiwng scheme definition (although my 'language' is not Scheme, it is the graph)
(define (foldr f z xs)
(if (null? xs)
z
(f (car xs) (foldr f z (cdr xs)))))
The problem is that there are since there are no special operators, everything, in particular if is just a normal function. In this form, the program never terminates and instead reaches the maximum stack depth, since the else clause is always computed.
I presume this problem is solved in some languages by lazy evaluation. So my questions are: is there functional version of fold that will not have this infinite recursion 2) where to begin thinking about applying lazy evaluation to a simple language such as this, if necessary.
ifis a special form in Scheme, so the else expression is not always evaluated. What's the question here? - Niklas B.ifspecially". - Niklas B.ifdifferently than other function calls (this is called a special form). You don't need full-blown lazy evaluation for that, like Haskell has (although you could implementif-then-elseas a simple function in Haskell). The statement by sepp that you can't writefoldif you don't have a lazyifwas not quite correct (and he deleted that comment already): You can emulate the behaviour by supplying lambdas to theifand calling the result. - Niklas B.