方案,函数列表作为参数

发布于 2024-11-01 16:52:40 字数 653 浏览 5 评论 0原文

我正在努力解决这个问题。我想知道是否有人可以帮助我开始它或给我一些提示。

名为 apply-all 的函数,当给定函数列表和数字时,将在应用于该数字时生成函数值列表。

例如, (apply-all (list sqrt squarecube) 4) =>; (2 16 64)

谢谢,

好的。这是我到目前为止所拥有的,

(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))))) 

但是当我运行它时,

(apply-all '(positive?) 2)

它给了我这个错误

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

有人能帮我找到问题吗?

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.

For example,
(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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入画浅相思 2024-11-08 16:52:40

迂腐队长说:你看过《如何设计程序》(http://www.htdp.org) 吗?

您需要从写下示例开始——比您现有的示例更简单。另外,以实际正确评估的形式编写结果。 (例如,在您的示例中,如果您评估 (2 16 64),您将收到错误。

接下来,如果您没有在列表上开发函数的经验,您真的应该阅读 HtDP 的前十部分; 这比 Stack Overflow 的答案要好得多,

希望有帮助!

Captain Pedantic says: have you taken a look at How To Design Programs (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.

Next, if you don't have experience developing functions over lists, you should really really be reading the first ten sections of HtDP; it's far better than a Stack Overflow answer can be.

Hope this helps!

瑾夏年华 2024-11-08 16:52:40

为了回应您的尝试,我将为您提供一些提示来帮助您完成任务。 :-)

  • 在您的情况下,您不需要使用 apply 。尽管您的作业希望您创建一个名为 apply-all 的函数,但 apply 并没有按照您的想法执行操作。
  • cons 有两个参数。
  • '(positive?) 是一个列表,其中包含名为 positive?符号,而不是 positive? 函数。您的作业使用 (list ...) 是有充分理由的。如果您想要比 list 更紧凑的内容,请使用准引用:`(,positive?)
  • 您应该考虑使用 map,就像 Marcin 的评论所建议的那样。
  • 如果您无法使用map,请记住,当您使用“使用累加器迭代”模式时,您的结果会相反。您必须反转输入列表或结果。

这是我的参考解决方案,花了我半分钟写的。 :-) 我希望您可以使用它来微调您现有的版本。 (我很乐意发布它,因为我确信您的标记不会让您使用 cut,并且如果您能弄清楚如何使我的版本为您的标记所接受,那么您就已经已经赢了。)

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

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 doesn't do what you think it does, notwithstanding that your assignment wants you to make a function called apply-all.
  • cons takes two arguments.
  • '(positive?) is a list that contains a symbol named positive?, not the positive? function. Your assignment used (list ...) for good reason. If you want something more compact than list, use quasiquotation: `(,positive?).
  • You should consider using map, like Marcin's comment suggests.
  • If you can't use map, then remember that when you use the "iterate with accumulator" pattern, your results come out reversed. 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.)

(define (apply-all fns . args)
  (map (cut apply <> args) fns))
究竟谁懂我的在乎 2024-11-08 16:52:40

给定签名:

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

考虑 apply-all 应该返回什么:

  • 当列表为空时:一个空列表(没有可应用的函数)
  • 当列表不为空时:对数字使用第一个函数,将结果与自然值组合以对列表有意义的方式进行递归(列表应在递归中缩小)。

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).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文