Louis Koo
Louis Koo

Reputation: 31

Using abstract list functions to iterate over a list of functions in Scheme

How can I write a function max-list-function that consumes a list of functions, then produces a function f such that for every x, (f x) produces the maximum value of all the functions g in the list of functions?

For example (max-list-function (lambda (n) (+ n 4)) (lambda (n) (- 15 n)))) produces a function such that (f 2) returns 13 and (f 10) returns 14.

This is to be done with abstract list functions (filter, foldr, map, ...) without recursion.

Upvotes: 1

Views: 297

Answers (1)

Óscar López
Óscar López

Reputation: 235984

Try this:

(define (max-list-function flist)
  (lambda (n)
    (foldr max -inf.0
           (map (lambda (f) (f n))
                flist))))

Use it like this:

(define f (max-list-function 
           (list (lambda (n) (+ n 4)) (lambda (n) (- 15 n)))))

(f 2)
> 13.0

(f 10)
> 14.0

Upvotes: 1

Related Questions