【发布时间】:2020-11-13 10:54:42
【问题描述】:
我正在尝试编写一个过程stream-weighted-tuples,它采用权重过程和任意数量的流来生成元组流。
例如,
(stream-weighted-tuples
(lambda (t) (+ (car t) (cadr t) (caddr t))
integers
integers
integers)
应该生成(1 1 1) (1 1 2) (1 2 1) (2 1 1) (1 1 3) (1 2 2) (1 3 1) (2 1 2) (2 2 1) (3 1 1) (1 1 4) (1 2 3) ...的流。
我受到 SICP 中的exercise 3.70 的启发,它是关于编写一个过程weighted-pairs,它采用两个流和一个权重过程来根据权重按顺序生成一对流。
所以基本上,这是对weighted-pairs 过程的概括,可以采用两个以上的流。
我写了以下版本:
(define (stream-weighted-tuples weight . streams)
(cond ((null? streams)
(error "No streams given -- STREM-WEIGHTED-TUPLES"))
((null? (cdr streams))
(stream-map list (car streams)))
(else
(let ((s (car streams))
(rest (cdr streams)))
(if (stream-null? s)
the-empty-stream ; {} x S = {}
(stream-merge-weighted
weight
(stream-map (lambda (tuple)
(cons (stream-car s) tuple))
(apply stream-weighted-tuples
(lambda (tuple) ; partial weight
(weight (cons (stream-car s)
tuple)))
rest))
(apply stream-weighted-tuples
weight
(stream-cdr s)
rest)))))))
(这显然不起作用)。
这个想法是合并 1. 由consing 第一个流的第一个元素产生的流与由 其余元组组成的(递归构造的)流的每个元素给定流的数量,以及 2. 由第一个流的stream-cdr 的元组和其余给定流的元组组成的流。
这不起作用,因为在无限流的情况下生成流 2 不会停止(与 exercise 3.68 中的 pairs 过程不工作的原因相同)。
这是我实现的stream-merge-weighted:
(define (stream-merge-weighted weight s1 s2)
(cond ((stream-null? s1) s2)
((stream-null? s2) s1)
(else
(let* ((s1car (stream-car s1))
(s2car (stream-car s2))
(s1car-weight (weight s1car))
(s2car-weight (weight s2car)))
(cond ((<= s1car-weight s2car-weight)
(cons-stream
s1car
(stream-merge-weighted weight
(stream-cdr s1)
s2)))
((> s1car-weight s2car-weight)
(cons-stream
s2car
(stream-merge-weighted weight
s1
(stream-cdr s2)))))))))
有没有办法递归构造这个问题?
编辑:我在这个问题中所说的“元组”实际上是一个语义上表示元组的方案列表。
作为参考,我保留了流原语的实现(与 SICP 中的实现相同):
(define-syntax cons-stream
(syntax-rules ()
((_ a b)
(cons a (delay b)))))
(define (stream-car stream)
(car stream))
(define (stream-cdr stream)
(force (cdr stream)))
(define (stream-null? stream)
(null? stream))
(define the-empty-stream '())
(define (stream-map proc s)
(if (stream-null? s)
the-empty-stream
(cons-stream
(proc (stream-car s))
(stream-map proc (stream-cdr s)))))
【问题讨论】:
标签: recursion stream scheme sicp lazy-sequences