0

I have to create a function in Scheme that takes in a value X, a list of functions, and returns a list of X's applied to those functions. For example:

(f1 f2 ... fn) and x ==> ((f1 x) (f2 x) ... (fn x))

I'm able to use map to do this. I know how to apply a list of functions to another list:

(define (myMap f_list lst)
  (if (null? f_list) lst
      (map (car f_list)
           (myMap (cdr f_list) lst))))

Is there anyway to alter this to allow me what I need?

1
  • is this a duplicate of stackoverflow.com/questions/10665906/… and are you taking the same course? if so, you'll need to do the map yourself. There is good advice in the answers over there. Commented Dec 7, 2013 at 4:39

2 Answers 2

2

you mean like this?

(define (applyAllTo fns x)
  (map (lambda (fn) (fn x)) fns))

then

(applyAllTo (list (lambda (x) (* 2 x)) (lambda (x) (* 3 x))) 5)

==> (10 15)
Sign up to request clarification or add additional context in comments.

Comments

0

you write:

create a function in Scheme that takes in a value X, a list of functions, and returns a list of X's applied to those functions.

First of all the function that you show isn't quite right:

(define (myMap f_list lst)
  (if (null? f_list) 
      lst             

really? return the 2nd argument if the 1st is an empty list? And if the 1st argument is a list of functions - judging from its name - why the 2nd is also called lst? Shouldn't it be x? And if it is, do we really want it returned when the list of functions is empty? No, when the list of functions is empty, there's nothing to apply our value to, so the overall list of results of applying x to each function in the list is ... an empty list, right? So,

(define (myMap f_list x)

the order of arguments is not important. You can change it later.

  (if (null? f_list) 
      '()
      (cons                            ; construct new list node

here you had map. Why? We're defining our own map-like function here. map-like functions construct an output list node by node, from results produced in a certain way from the values in the input list, node by node. I.e. we access the input list node by node, and construct the list of results node by node. Using the cons function .

            ( .... (car f_list) .... ) ; something to do with car

cons that on top of

            (myMap (cdr f_list) x) )))

this part is right. So what do we do with the car of f_list? What is it? A function to be called. So we just call it - apply x to it. The function application syntax in Scheme is just (function argument_value). That's it.


Now you can do even more than what was asked in the assignment. For example, you can write a function that will apply each function in the input list twice to the given argument. It's easy to do, following the same general code skeleton:

(define (maplike_func a_list param)  ; args order non-important
  (if (null? a_list)
      '()
      (cons (do_something (car a_list) param)
            (maplike_func (cdr a_list) param))))

In fact this skeleton is an instance of another, even more general pattern of folding:

(define (right_fold do_what on_null a_list)
  (if (null? a_list)
      on_null
      (do_what (car a_list)
               (lambda () (right_fold do_what on_null (cdr a_list))))))

With it, our function can be expressed as

(define (myMap f_list x) 
  (right_fold 
      (lambda (a r) (cons (a x) (r)))
      '()
      f_list))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.