gpt4 book ai didi

functional-programming - Scheme/Lisp 嵌套循环和递归

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:00 24 4
gpt4 key购买 nike

我正在尝试解决 Scheme 中的一个问题,该问题要求我使用嵌套循环或嵌套递归。

例如我有两个列表,我必须检查它们的笛卡尔积的条件。

处理这类问题的最佳方法是什么?关于如何简化这些类型的函数的任何指示?


我会详细说明一下,因为我的意图可能不够清楚。

常规递归函数可能如下所示:

(define (factorial n)
(factorial-impl n 1))

(define (factorial-impl n t)
(if (eq? n 0)
t
(factorial-impl (- n 1) (* t n))))

尝试编写一个类似的函数但使用嵌套递归会给代码带来新的复杂性,我想知道这些类型的函数的基本模式是什么,因为它可能变得非常丑陋,速度非常快。

作为一个具体示例,我正在寻找访问两个列表的笛卡尔积中所有项目的最简单方法。

最佳答案

在方案中,“ map ”函数通常可以方便地计算一个基于另一个列表的列表。

事实上,在 scheme 中,map 接受一个“n-argument”函数和“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)))

我认为上面的内容相当“优雅”……直到有人向我展示了等效的 Haskell 定义:

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 行!!!

我对我的实现效率不太有信心 - 特别是“展平”步骤写起来很快,但最终可能会调用“追加”有很多列表,在某些方案上可能非常有效,也可能不是很有效实现。

为了最终的实用性/有用性,您需要一个可以采用“延迟评估”列表/流/迭代器而不是完全指定列表的版本......如果您愿意,可以使用“笛卡尔映射流”函数,然后返回结果的“流”......但这取决于上下文(我正在考虑 SICP 中引入的“流”概念)......并且由于它的惰性评估,Haskell 版本将免费提供。

一般来说,在 Scheme 中,如果你想在某个时候“中断”循环,你也可以使用延续(比如抛出异常,但在 Scheme 中这是控制流的公认做法)。

我写这篇文章很开心!

关于functional-programming - Scheme/Lisp 嵌套循环和递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1658229/

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