方案中的列表
我正在尝试在方案中编写一个函数,该函数接受一个列表并对列表中的每个项目进行平方,然后以 (list xyz)
的形式返回列表。但是,我不确定如何编写可以做到这一点的代码。到目前为止,我已经做到了,
(define (square=list list)
(cond
[(empty? list) false]
[else (list (sqr (first a-list))(square-list (rest a-list)))]))
但它返回了以下形式的列表:
(cons x (cons y (cons z empty)))
我该怎么做才能使其仅以 (list xyz)
形式返回列表?谢谢!
I'm trying to write a function in scheme that takes a list and squares every item on the list, then returns the list in the form (list x y z)
. However, I'm not sure how to write a code that will do that. So far, I have
(define (square=list list)
(cond
[(empty? list) false]
[else (list (sqr (first a-list))(square-list (rest a-list)))]))
but it returns the list in the form
(cons x (cons y (cons z empty)))
What can I do to make it return the list just in the form (list x y z)
? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你就快到了——确保你理解
cons
和list
之间的区别(教科书 如何设计程序 在第 13 节中对此进行了解释。您可以找到在线副本 此处)。cons 会将一个项目作为其第一个元素,(通常)将一个(可能为空)列表作为“其余”部分。例如,
(cons 1empty)
将数字 1 作为其第一个元素,将empty
列表作为“其余”元素。(cons 1 (cons 2empty))
将数字 1 作为第一个元素,将(cons 2empty)
作为“其余”元素。list
只是创建列表的简单简写,可以获取任意数量的项目。因此:(list 1 2 3 4 5)
相同
与...
'(1 2 3 4 5)
,与
(cons 1 (cons 2 (cons 3 (cons 4 (cons 5 空)))))
。但要小心。
(list 1 (list 2 (list 3)))
与(cons 1 (cons 2 (cons 3empty))) 不相同>。事实上,它是
(cons 1 (cons 2 (cons 3empty)empty)empty)
。如果您仍然感到困惑,请随时发表评论。
You're almost there -- make sure you understand the difference between
cons
andlist
(the textbook How to Design Programs explains this in Section 13. You can find the online copy here).cons
will take an item as its first element and (usually) a (possibly empty) list for the 'rest' part. As an example,(cons 1 empty)
has the number 1 as its first element and theempty
list as the 'rest'.(cons 1 (cons 2 empty))
has the number 1 as the first element, and(cons 2 empty)
as the 'rest'.list
is just an easy shorthand for making lists, taking an arbitrary number of items. Thus:(list 1 2 3 4 5)
is the same as...
'(1 2 3 4 5)
which is the same as
(cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty)))))
.But be careful.
(list 1 (list 2 (list 3)))
is not the same as(cons 1 (cons 2 (cons 3 empty)))
. In fact, it is(cons 1 (cons 2 (cons 3 empty) empty) empty)
.If you're still confused, feel free to post a comment.
问题是您在 else 语句中使用列表。您是说为我构建一个列表,将此值作为第一个条目,并将列表作为第二个条目。
您想要将第一个条目添加到由递归调用创建的列表中。
The problem is that you're using list in the else statement. You are saying build me a list with this value as the first entry, and a list as the second entry.
You want to cons the first entry onto the list created by recursive call.
这可能不是你的助教正在寻找的,但无论如何我都会把它放进去,因为它可能会帮助你更多地理解Scheme。编写您想要执行的操作的惯用方式(在Scheme中)是使用
map
:map
应用一个函数(这里,(lambda (x) ( * xx))
- 一个无名函数,它将其输入的平方返回到列表的每个元素,并返回一个包含所有结果的新列表。 Scheme 的map
基本上与您在代码中执行的迭代相同,优点是通过使用map
您永远不必显式编写此类迭代(并且,至少名义上,方案实现可能会以某种特殊的方式优化映射,尽管在大多数情况下这并不是那么重要)。关于 map 的重要之处在于,它将您的代码简化为重要部分 - 该代码对列表中的每个元素求平方,另一个代码取每个元素的平方根,该代码向其添加 1 等等,而无需重复相同的基本循环迭代代码一次又一次。This is probably not what your TA is looking for, but I'll throw it in anyway because it may help you grok a tiny bit more of Scheme. The idiomatic (in Scheme) way to write what you are trying to do is to use
map
:map
applies a function (here,(lambda (x) (* x x))
- a nameless function that returns the square of its input) to each element of a list and returns a new list containing all of the results. Scheme'smap
basically does the same iteration you are doing in your code, the advantage being that by usingmap
you never have to explicitly write such iterations (and, nominally at least, a Scheme implementation might optimize map in some special way, though that's not really that important in most cases). The important thing about map is that it reduces your code to the important parts - this code squares each element of the list, this other code takes the square root of each element, this code adds one to it, etc, without having to repeat the same basic loop iteration code again and again.