将函数列表映射到列表 [英] Map a list of functions to a list

查看:41
本文介绍了将函数列表映射到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是作业,所以我不想要答案.我只需要朝着正确的方向推动.我需要将多个函数映射到一个列表上.例如:

This is homework, so I don't want the answer. I only need a push in the right direction. I am required to map multiple functions onto a list. For instance:

(map-multi (list plus-one square) '(4 5 6)) => (25 36 49)

我能够将第一个函数映射到列表的元素,但是,在那之后我很迷茫.另外,由于这是介绍性的,我仅限于介绍性函数(constappendcarcdrmember 等)

I am able to have it map the first function to the elements of the list, however, I get very lost after that. Also, since this is introductory, I am limited to introductory functions (const, append, car, cdr, member, etc.)

(define (map-multi f l)  
    (cond  
        ((null? l)  
            l)  
        (else (cons ((car f) (car l))  
            (map-multi f (cdr l))))))  

推荐答案

您需要组合 f 参数中接收到的函数.为简单起见,假设列表中只有两个函数 - 然后您需要将第一个函数应用于数字列表中的当前元素,然后将第二个函数应用于其结果.如果您可以使用 compose 过程,请继续它并在您的代码中更改这一行:

You need to compose the functions you receive in the f parameter. For simplicity's sake, let's say that there are only two functions in the list - then you need to apply the first function to the current element in the list of numbers and then apply the second function to the result of that. If you can use the compose procedure go ahead with it and change this line in your code:

((car f) (car l)) ; you're applying only the 1st function! what about the 2nd?

...与这个:

((compose (cadr f) (car f)) (car l))       ; now we're applying both functions

如果你不能使用compose,那么用这一行替换同一行:

If you can't use compose, then replace the same line with this one:

((cadr f) ((car f) (car l)))               ; now we're applying both functions

现在,如果问题更普遍,并且您需要映射具有两个以上元素的函数列表,那么再次将代码中的同一行替换为:>

Now, if the problem is more general and you're required to map a list of functions with more than two elements, then once more replace the same line in your code with this:

((compose-multi f) (car l))

并通过连续调用compose来实现一个辅助函数,该函数组合并返回列表中的所有函数.考虑到它是家庭作业,这个留给您作为练习 - 但如果您了解上述代码如何仅适用于两个函数,那么扩展多个函数列表的结果应该很容易:

And implement a helper function that composes and returns all the functions in the list, by successive calls to compose. This one is left as an exercise for you, given that it's homework - but if you understand how the above code works for just two functions, it should be easy enough to extend the result for a list of multiple functions:

(define (compose-multi flist)      ; procedure for composing a list of functions
  (if (null? flist)                ; if the list is empty then
      <???>                        ; return the identity function
      (<???> (compose-multi <???>) ; else compose the result of recursive call
             <???>)))              ; with the current element in the list

注意,处理函数列表中没有元素的情况需要恒等函数;定义非常简单,它只返回作为参数传递的相同值.

Notice that the identity function is required for handling the case where there are no elements in the list of functions; it's very simple to define, it just returns the same value that was passed as parameter.

还要注意 compose-multi 返回一个 function,即组合列表中所有函数的结果 - compose 这样做是为了你,但如果你不被允许使用它,请记住这一点:

Also be aware that compose-multi returns a function, the result of composing all the functions in the list - compose does this for you, but if you're not allowed to use it just remember that this:

(compose x y)

... 等价于:

(lambda (n) (x (y n)))

这篇关于将函数列表映射到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆