I have a coding class assignment i am not able to solve :(
(define f
(lambda (x)
(i (g x) (h (g x)))))
i, g and h are just arbitrary function names.
This is the code and i am required to rewrite it such that (g x)
is evaluated only once but without using let (or any variant of it) using only define
and lambda
as predefined functions. I am also not allowed to take calculations out of f
, that is, alls calculations must happen inside that function.
The easy way is
(define f
(lambda (x)
(define intermediary (g x))
(i intermediary (h intermediary))))
and the more complicated would be
(define f
(lambda (x)
((lambda (intermediary) ; anonymous procedure
(i intermediary (h intermediary)))
(g x))))
or, avoiding the anonymous procedure and giving it the name sub:
(define f
(lambda (x)
(define sub
(lambda (intermediary)
(i intermediary (h intermediary))))
(sub (g x))))