作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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) )
我怎样才能做到这一点?最好有高阶函数?
最佳答案
这是一个完全使用高阶函数的方法(foldr
、append-map
和 map
;现在也使用 compose1
、curry
和 curryr
):
(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/
我是一名优秀的程序员,十分优秀!