(方案)使用 do cicle 验证一个列表中的元素是否在第二个列表中
我们如何在方案中使用 do cicle 验证第一个列表的元素是否在第二个列表中?
How do we verify in scheme with the do cicle, if an element of the first list is in the second?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
racket 中的 do 循环有一个有趣的结构:
r5rs 的文档提供了一个示例:
该语句返回 25,即循环元素的总和。 do循环中的x被初始化为let中的x,然后每次循环迭代设置为自身的cdr 。
sum
初始化为0,每次累加x
的car
的值。停止条件是迭代变量为空,返回值是和。好吧,除了方括号的球拍偏好之外,这看起来不错。有一个 do 循环和一个列表。循环对该列表执行一些操作。我们可以用它来编写一个在列表中查找特定原子的函数(使用球拍括号):
我不是初始化并添加值
sum
,而是or
到 <代码>找到。另外,我更喜欢first
和rest
而不是car
和cdr
,并在它们不存在时自己定义它们。该函数的工作方式应遵循示例的解释。正如预期的那样,给出#f。同样:
给出#t。
您是否能够将使用 do 循环在列表中查找特定元素概括为您的特定问题?
The do loop in racket has an interesting structure:
The documentation for r5rs provides an example:
That statement returns 25, the sum of the elements of the loop. The
x
in the do loop is initialized to thex
in the let, and then iteratively set to thecdr
of itself each time through the loop.sum
is initialized to 0, and accumulates the value of thecar
ofx
each time through. The stopping condition is when the iteration variable is empty, and the return value is the sum.Ok, aside from the racket preference of square brackets, this looks good. There's a do loop and a list. The loop does something over that list. We can use that to write a function that finds a specific atom in a list (using the racket brackets):
Instead of initializing and adding the value
sum
, Ior
intofound
. Also, I preferfirst
andrest
overcar
andcdr
and define them myself when they don't exist. The way this function works should follow from the explanation of the example.Gives #f, as expected. Similarly:
Gives #t.
Are you able to generalize finding a specific element in a list with a do loop into your specific question?