0

I'm trying to modify the function below to compose two functions in Scheme.

(define (compose F1 F2)
    (eval F1 (interaction-environment))
)

rather than

(define (compose f g)
  (λ (x) (f (g x))))

But I'm not sure about how to use eval.

Morovo
  • 3
  • 1
  • 3

1 Answers1

0

From your suggestion, I guess you want to use Scheme's macros / preprocessing capabilities. eval isn't meant for code transformation. Composition can be defined in Scheme as

(define (∘ f g)
        (lambda (x) (f (g x))) )

or

(define-syntax ∘
   (syntax-rules ()
      ((∘ f g)
       (lambda (x) (f (g x))) )))

where the arity of expressions f and g is 1.

(define (plus-10 n) (+ n 10))
(define (minus-3 n) (- n 3))

(display
   (map (∘ plus-10 minus-3)
        (list 1 2 3 4) ))

The map expression at compile-time becomes

(map (lambda (x) (plus-10 (minus-3 x)))
     (list 1 2 3 4) )

equal?s

(list 8 9 10 11)