我如何以这种方式在方案中生成排列:排列的第一个元素正好在 1-2 之间,第二个 1-4 和第三个 1-3 ......而且数字可以在排列中出现不止一次
问问题
922 次
2 回答
0
使用 的排列for/list
,在 Racket 文档中进行了介绍。
(for/list ([i '(1 2)]
#:when i
[j '(1 2 3 4)]
#:when j
[k '(1 2 3)])
(list i j k))
于 2013-12-03T16:42:12.870 回答
0
Here's how you'd generate all the permutations for a sequence of numbers in a given range:
(define (cartesian-product . lsts)
(foldr (lambda (lst acc)
(for*/list ((x (in-list lst))
(y (in-list acc)))
(cons x y)))
'(())
lsts))
(cartesian-product (range 1 3) (range 1 5) (range 1 4))
=> '((1 1 1) (1 1 2) (1 1 3) (1 2 1) (1 2 2) (1 2 3) (1 3 1) (1 3 2)
(1 3 3) (1 4 1) (1 4 2) (1 4 3) (2 1 1) (2 1 2) (2 1 3) (2 2 1)
(2 2 2) (2 2 3) (2 3 1) (2 3 2) (2 3 3) (2 4 1) (2 4 2) (2 4 3))
于 2013-04-07T15:43:14.050 回答