[英]Creating a list of lists in Racket
I am new to Racket programming, and I am working on a problem where I am given a list of numbers, and I have to make a list of list, of different combinations of numbers. 我是Racket编程的新手,我正在研究一个给我一个数字列表的问题,我必须制作一个列表的列表,其中包含数字的不同组合。
Something like : 就像是 :
(combine (list 3 1 2)) => (list
(list 31 32 33)
(list 21 22 3)
(list 11 12 13))
How do I achieve this in Racket? 如何在球拍中实现这一目标? Thank You 谢谢
Just play with iterators and comprehension to implement a cartesian product that returns lists of lists, and a bit of arithmetic to obtain the right results. 只需玩弄迭代器和理解即可实现笛卡尔乘积,该乘积返回列表列表,并进行一些算术运算以获得正确的结果。 Try this: 尝试这个:
(for/list ((i '(3 2 1)))
(for/list ((j '(1 2 3)))
(+ (* 10 i) j)))
Or alternatively, using more standard constructs (available in student languages): 或者,使用更标准的结构(以学生语言提供):
(map (lambda (i)
(map (lambda (j)
(+ (* 10 i) j))
'(1 2 3)))
'(3 2 1))
Either way, it works as expected: 无论哪种方式,它都能按预期工作:
=> '((31 32 33) (21 22 23) (11 12 13))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.