方案问题(使用函数作为参数)
我是一名计划新手,正在努力理解我的作业。 我之前创建了一个名为 duplicate 的函数,它看起来像这样:
( DEFINE ( duplicate lis )
(IF (NULL? lis) '())
((CONS (CAR lis) (CONS (CAR lis) (duplicate (CDR lis))))
))
典型的 i/o 是 i: (duplicate '(1 2 3 4)) o: (1 1 2 2 3 3 4 4),所以基本上它会复制列表中的所有内容。 继续: 现在我应该创建一个名为 comp 的函数。 它应该是这样构建的:
(DEFINE (comp f g) (lambda (x) (f (g (x))))
我可以在其中输入 '(1 2 3 4) 它会返回 (1 1 4 4 9 9 16 16)
所以 f = duplicate 和 g = 拉姆达。 我知道 lambda 可能看起来像这样:
(lambda (x) (* x x))
但这就是问题开始的地方,我已经在这上面花了几个小时,正如你所看到的,没有取得太大进展。
任何帮助将不胜感激。 此致。
I'm a Scheme newbie and trying to make sense of my homework.
I've a function I made earlier called duplicate, and it looks like this:
( DEFINE ( duplicate lis )
(IF (NULL? lis) '())
((CONS (CAR lis) (CONS (CAR lis) (duplicate (CDR lis))))
))
A typical i/o from this would be i: (duplicate '(1 2 3 4)) o: (1 1 2 2 3 3 4 4), so basicly it duplicates everything in the list.
Moving on:
Now I'm supposed to make a function that's called comp.
It's supposed to be built like this:
(DEFINE (comp f g) (lambda (x) (f (g (x))))
Where I could input '(1 2 3 4) and it would return (1 1 4 4 9 9 16 16)
so f = duplicate and g = lambda.
I know lambda should probably look like this:
(lambda (x) (* x x))
But here's where the problem starts, I've already spent several hours on this, and as you can see not made much progress.
Any help would be appreciated.
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
map
:或者修改
duplicate
以将过程作为其第二个参数并将其应用于列表的每个元素:Use
map
:or, modify
duplicate
to take a procedure as its second argument and apply it to each element of the list:一种方法如下:
现在在 repl 中,执行以下操作:
One way to do is as follows:
Now at the repl, do: