gpt4 book ai didi

dictionary - Racket 映射笛卡尔积事物

转载 作者:行者123 更新时间:2023-12-03 10:00:22 24 4
gpt4 key购买 nike

在 racket 中,像 map 这样的高阶函数用在两个列表上是这样做的:

(map list '(1 2 3) '(1 2 3))
> '( (1 1) (2 2) (3 3) )

但我想要像这样的笛卡尔积:

'( (1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3) )

我怎样才能做到这一点?最好有高阶函数?

最佳答案

这是一个完全使用高阶函数的方法(foldrappend-mapmap;现在也使用 compose1currycurryr):

(define (cartesian-product . lists)
(foldr (lambda (a b)
(append-map (compose1 (curryr map b) (curry cons))
a))
'(())
lists))

请原谅可怕的参数名称。总有一天我会想出一件好事。 :-)

关于dictionary - Racket 映射笛卡尔积事物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27770566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com