Great !!!
Been thinking about your previous post -- there may be very clever ways in Lisp to handle mutual , nested or whatever recursion. After all Lisp is famous for trampolining , which in fact means that there is a kind of "mother function" which in an iterative way outputs not values but functions -- wrote something , the functions generated are not evaluated till a certain condition is reached , it are just patterns , here an index has been used , but of course a more general (while ) can be used too , or a (catch (throw ) construction or whatever ........
;;------------------------------------------------------------------------------
>
(define fac
(lambda (x)
(let ((pattern (list x '*))
(print-it-nice (lambda (m)
(let ((fi (cons 'fac (first m))))
(reverse (cons fi (rest m)))))))
(dotimes (i (- x 1))
(push (- (first pattern) 1) pattern)
(println (print-it-nice pattern)))
(eval (reverse (rest pattern))))))
> (fac 10)
(* 10 (fac 9))
(* 10 9 (fac
)
(* 10 9 8 (fac 7))
(* 10 9 8 7 (fac 6))
(* 10 9 8 7 6 (fac 5))
(* 10 9 8 7 6 5 (fac 4))
(* 10 9 8 7 6 5 4 (fac 3))
(* 10 9 8 7 6 5 4 3 (fac 2))
(* 10 9 8 7 6 5 4 3 2 (fac 1))
3628800
Important : only the last list is evaluated , nothing is calculated in-between (in fact , it can't be computed ) !!!! , nothing is stacked somewhere -- in memory only the length of the list you see is needed.....
(infact I'm using a dirty trick -- the lists I use are reversed during computing, with the operator coming at the tail -- avoiding evaluating
best Rob
(only an example of a function generating functions -- for x! the more compact way is :
(define fac (lambda (x) (apply * (sequence 1 x))))