wzsun
wzsun

Reputation: 425

Scheme - create a function from a list of functions

Hey I'm having trouble thinking about this and would like some help.

In Scheme if I'm given a list of functions (list square - double) I need to make a function that will encompass the list

For example

(let (f (co (list square - double))))  where co is the function name that combines

would be the same as

(square (- (double n))) where n is some number
So you can do the following
(f 2) => (16) 

(define (co functions) (lambda (n) (? functions)))

I'm not sure where to go from the ?. I know if you map it you end up getting the functions applied to the number but output as a list so it would be '(4 -2 4).

Any ideas would be appreciated

Upvotes: 0

Views: 81

Answers (1)

吴烜_中文编程
吴烜_中文编程

Reputation: 548

Hopefully it's not a homework.

> (define double (lambda (n) (* n 2)))
> (define square (lambda (n) (* n n)))
> (set! fs (list square - double))
> (define co
    (lambda (functions n)
      (cond 
        ((null? functions) n)
        (else ((car functions) (co (cdr functions) n))))))
> (co fs 6)
144
> (co fs 4)
64
> (co fs 2)
16

Upvotes: 1

Related Questions