在Scheme中, “map”函数通常用于计算基于另一个列表的一个列表。
事实上,在方案中,地图需要一个“正论证”功能和“n”名单,并调用 功能为每个列表中的每个对应的元素:
> (map * '(3 4 5) '(1 2 3))
(3 8 15)
但一个很自然的除这将是一个“笛卡尔映射”函数,它将用你从每个列表中选取一个元素的所有不同方式调用你的“n-argument”函数。我花了一段时间才能弄清楚到底如何做到这一点,但在这里你去:
; curry takes:
; * a p-argument function AND
; * n actual arguments,
; and returns a function requiring only (p-n) arguments
; where the first "n" arguments are already bound. A simple
; example
; (define add1 (curry + 1))
; (add1 3)
; => 4
; Many other languages implicitly "curry" whenever you call
; a function with not enough arguments.
(define curry
(lambda (f . c) (lambda x (apply f (append c x)))))
; take a list of tuples and an element, return another list
; with that element stitched on to each of the tuples:
; e.g.
; > (stitch '(1 2 3) 4)
; ((4 . 1) (4 . 2) (4 . 3))
(define stitch
(lambda (tuples element)
(map (curry cons element) tuples)))
; Flatten takes a list of lists and produces a single list
; e.g.
; > (flatten '((1 2) (3 4)))
; (1 2 3 4)
(define flatten
(curry apply append))
; cartesian takes two lists and returns their cartesian product
; e.g.
; > (cartesian '(1 2 3) '(4 5))
; ((1 . 4) (1 . 5) (2 . 4) (2 . 5) (3 . 4) (3 . 5))
(define cartesian
(lambda (l1 l2)
(flatten (map (curry stitch l2) l1))))
; cartesian-lists takes a list of lists
; and returns a single list containing the cartesian product of all of the lists.
; We start with a list containing a single 'nil', so that we create a
; "list of lists" rather than a list of "tuples".
; The other interesting function we use here is "fold-right" (sometimes called
; "foldr" or "reduce" in other implementations). It can be used
; to collapse a list from right to left using some binary operation and an
; initial value.
; e.g.
; (fold-right cons '() '(1 2 3))
; is equivalent to
; ((cons 1 (cons 2 (cons 3 '())))
; In our case, we have a list of lists, and our binary operation is to get the
; "cartesian product" between each list.
(define cartesian-lists
(lambda (lists)
(fold-right cartesian '(()) lists)))
; cartesian-map takes a n-argument function and n lists
; and returns a single list containing the result of calling that
; n-argument function for each combination of elements in the list:
; > (cartesian-map list '(a b) '(c d e) '(f g))
; ((a c f) (a c g) (a d f) (a d g) (a e f) (a e g) (b c f)
; (b c g) (b d f) (b d g) (b e f) (b e g))
(define cartesian-map
(lambda (f . lists)
(map (curry apply f) (cartesian-lists lists))))
没有所有的评论和一些更紧凑的功能定义语法有:
(define (curry f . c) (lambda x (apply f (append c x))))
(define (stitch tuples element)
(map (curry cons element) tuples))
(define flatten (curry apply append))
(define (cartesian l1 l2)
(flatten (map (curry stitch l2) l1)))
(define cartesian-lists (curry fold-right cartesian '(()))))
(define (cartesian-map f . lists)
(map (curry apply f) (cartesian-lists lists)))
我以为以上是合理的“优雅” ......直到有人向我展示了相当的哈斯克尔定义:
cartes f (a:b:[]) = [ f x y | x <- a , y <- b ]
cartes f (a:b:bs) = cartes f ([ f x y | x <- a , y <- b ]:bs)
2线!
我不是那么有信心在我的执行效率 - 尤其是“扁平化”的步骤是快写,但最终可能会调用“追加” 一个非常大的数量的列表,这可能会或可能不会很在一些Scheme实现上效率很高。
为了达到最终的实用性/实用性,您需要一个可以采用“懒惰评估”列表/流/迭代器而不是完全指定列表的版本....如果您喜欢,可以使用“笛卡尔地图流”功能然后返回结果的“流”......但这取决于上下文(我正在考虑在SICP中引入的“流”概念)......并且由于它的延迟评估而免费来自Haskell版本。一般来说,在Scheme中,如果你想在某个时间点“循环”循环,你也可以使用延续(例如抛出一个异常,但在控制流程的Scheme中被接受)。
我很开心写这个!
你的问题不是很具体。你能否提供更多关于你想要做什么以及你遇到什么问题的信息? – 2009-11-01 20:32:54
也许你可以尝试用SRFI-42编写它,并在此张贴,以证明你想完成什么? – grettke 2009-11-01 23:40:21