方案,函数列表作为参数
我正在努力解决这个问题。我想知道是否有人可以帮助我开始它或给我一些提示。
名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迂腐队长说:你看过《如何设计程序》(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!
为了回应您的尝试,我将为您提供一些提示来帮助您完成任务。 :-)
apply
。尽管您的作业希望您创建一个名为apply-all
的函数,但apply
并没有按照您的想法执行操作。'(positive?)
是一个列表,其中包含名为positive?
的符号,而不是positive?
函数。您的作业使用(list ...)
是有充分理由的。如果您想要比list
更紧凑的内容,请使用准引用:`(,positive?)
。map
,就像 Marcin 的评论所建议的那样。map
,请记住,当您使用“使用累加器迭代”模式时,您的结果会相反。您必须反转输入列表或结果。这是我的参考解决方案,花了我半分钟写的。 :-) 我希望您可以使用它来微调您现有的版本。 (我很乐意发布它,因为我确信您的标记不会让您使用
cut
,并且如果您能弄清楚如何使我的版本为您的标记所接受,那么您就已经已经赢了。)In response to your attempt, I'll provide you some hints to get you through. :-)
apply
in your case.apply
doesn't do what you think it does, notwithstanding that your assignment wants you to make a function calledapply-all
.cons
takes two arguments.'(positive?)
is a list that contains a symbol namedpositive?
, not thepositive?
function. Your assignment used(list ...)
for good reason. If you want something more compact thanlist
, use quasiquotation:`(,positive?)
.map
, like Marcin's comment suggests.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.)给定签名:
考虑 apply-all 应该返回什么:
Given the signature:
Consider what apply-all should return: