简体   繁体   English

方案,功能列表作为参数

[英]scheme, list of functions as parameter

I'm trying to solve this problem. 我正在尝试解决这个问题。 I was wondering if someone would help get started on it or give me some hints. 我想知道是否有人会帮助上手或给我一些提示。

function called apply-all that, when given a list of functions and a number, will produce a list of the values of the functions when applied to the number. 称为apply-all的函数,当给出一个函数列表和一个数字时,将在应用于该数字时产生一个函数值的列表。

For example, (apply-all (list sqrt square cube) 4) => (2 16 64) 例如, (apply-all (list sqrt square cube) 4) =>(2 16 64)

Thanks 谢谢

OK. 好。 this is what I have so far, 这就是我到目前为止

(define (apply-all lst num)
  (apply-allaux lst num '()))

;; aux function
(define (apply-allaux lst num acc)
  (if (null? lst)
      acc
      (apply-allaux (cdr lst) num (cons (apply (car lst) num))))) 

but when I run this 但是当我运行这个

(apply-all '(positive?) 2)

it gives me this error 它给了我这个错误

mcar: expects argument of type <mutable-pair>; given 2

Can anyone help me find the problem please? 谁能帮我找到问题吗?

Captain Pedantic says: have you taken a look at How To Design Programs (http://www.htdp.org) ? Pedantic上尉说:您是否看过“如何设计程序”(http://www.htdp.org)?

You need to start by writing down examples--simpler ones than the one you have. 您需要首先编写示例-比您拥有的示例简单一些。 Also, write the result in a form in which it actually evaluates correctly. 另外,将结果以可以正确评估的形式编写。 (In your example, for instance, if you evaluate (2 16 64), you'll get an error. (例如,在您的示例中,如果您求值(2 16 64),则会得到一个错误。

Next, if you don't have experience developing functions over lists, you should really really be reading the first ten sections of HtDP; 接下来,如果您没有在列表上开发功能的经验,那么您真的应该阅读HtDP的前十部分。 it's far better than a Stack Overflow answer can be. 它比堆栈溢出的答案要好得多。

Hope this helps! 希望这可以帮助!

In response to your attempt, I'll provide you some hints to get you through. 为了响应您的尝试,我将为您提供一些提示,以帮助您顺利通过。 :-) :-)

  • You don't need to use apply in your case. 在您的情况下,您无需使用apply apply doesn't do what you think it does, notwithstanding that your assignment wants you to make a function called apply-all . 尽管您的分配希望您创建一个名为apply-all的函数, apply并没有像您想的那样工作。
  • cons takes two arguments. cons有两个参数。
  • '(positive?) is a list that contains a symbol named positive? '(positive?)是包含名为positive?符号的列表positive? , not the positive? ,不是positive? function. 功能。 Your assignment used (list ...) for good reason. 您的作业有充分的理由使用了(list ...) If you want something more compact than list , use quasiquotation: `(,positive?) . 如果您想要比list更紧凑的内容,请使用准引用: `(,positive?)
  • You should consider using map , like Marcin's comment suggests. 您应该考虑使用map ,就像Marcin的评论所建议的那样。
  • If you can't use map , then remember that when you use the "iterate with accumulator" pattern, your results come out reversed. 如果您不能使用map ,那么请记住,当您使用“使用累加器迭代”模式时,结果反转。 You must either reverse the input list or the result. 您必须反转输入列表或结果。

Here's my reference solution, which took me half a minute to write. 这是我的参考解决方案,花了我半分钟时间写。 :-) I'm hoping you can use it to fine-tune your existing version. :-)我希望您可以使用它来微调您的现有版本。 (I'm comfortable with posting it because I'm certain your markers won't let you use cut , and if you can work out how to make my version acceptable to your markers, then you've already won.) (我很乐意发布它,因为我敢肯定您的标记不允许您使用cut ,如果您能确定如何使我的版本可以被标记接受,那么您已经赢了。)

(define (apply-all fns . args)
  (map (cut apply <> args) fns))

Given the signature: 给定签名:

; apply-all : (listof (number -> number)), number -> (listof number)

Consider what apply-all should return: 考虑一下应用所有应返回的内容:

  • when the list is empty: an empty list (there are no functions to apply) 当列表为空时:一个空列表(没有适用的功能)
  • when the list is not empty: use the first function on the number, combine the result with a natural recursion in a way that makes sense for lists (the list should shrink in the recursion). 当列表不为空时:使用数字上的第一个函数,以对列表有意义的方式将结果与自然递归组合(列表应在递归中收缩)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM