在方案中生成列表列表的函数
我正在尝试使用方案编写一个函数f
,它接受一个数字n
和一个函数g
并返回一个长度列表的列表n
,但根据 g
指示的模式与布尔值一致。例如,函数 f
应采用 n(例如 3),而函数 g
则使列表中的每个第 3 项都为 true。它应该返回:
(list (list true true false)
(list true true false)
(list true true false))
我不知道从哪里开始,所以任何帮助或提示将不胜感激。谢谢!
I'm trying to use scheme to write a function f
that takes a number n
and a function g
and returns a list of lists of length n
, but according with booleans according to the pattern indicated by g
. For example, the function f
should take n say 3 and the function g
which makes every 3rd item on the list a true. It should return this:
(list (list true true false)
(list true true false)
(list true true false))
I have no idea where to start with this, so any help or tips would be greatly appreciated. THanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键是使用
map
。简而言之,
map
接受一个函数和一个列表,并将该函数应用于该列表,返回修改后的列表。为此,我假设您不需要直接的解决方案,我首先构建您的列表列表(只需使用累加器 - 倒计时 - 使列表根据需要长,然后使用 < code>cons 和
list
和quote
对于其他所有内容,然后只需将 map (可能两次,具体取决于您实现解决方案的方式)应用于列表列表。 (例如列表
l
和函数fn
:(map fn (car l))
并使用cdr
重复l
和cons
结果一起返回)祝你好运!
Key to this looks like using
map
.In short,
map
takes a function and a list, and applies the function to that list, returning the modified list.To do this, as I'm assuming you don't want a direct solution, I'd first construct your list of lists (just use an accumulator -- count down -- to make the list as long as needed, and use
cons
andlist
andquote
for everything else.Then simply apply map (probably twice, depending on how you implement your solution) to the list of lists. (eg list
l
and functionfn
:(map fn (car l))
and recur with thecdr
ofl
andcons
the results back together).Good luck! Hope this helps!
我不太明白你想要做什么,但除了
map
之外,你可能会发现build-list
很有用。build-list
采用一个数字和一个过程,采用从 0 到该数字减 1 的范围,并将您的过程映射到该范围。例如I don't quite follow what you're trying to do, but in addition to
map
, you might findbuild-list
to be useful.build-list
takes a number and a procedure, takes the range from 0 to that number less 1, and maps your procedure over that range. e.g.