给出了顺序流和数据流,我需要根据order-stream对数据进行排序。
#lang racket
(define the-empty-stream '())
(define (stream-car stream)
(car stream))
(define-syntax cons-stream
(syntax-rules ()
((cons-stream x y)
(cons x (delay y)))))
(define (stream-cdr stream)
(force (cdr stream)))
(define stream-null? null?)
(define (integers-starting-from n)
(stream-cons n (integers-starting-from (+ n 1))))
(define integers (integers-starting-from 1))
(define order-stream (stream-cons 2 1))
(define data-stream (stream-cons 5 6))
(define (reorder order-stream data-stream)
(cond ((stream-null? order-stream) the-empty-stream)
((stream-null? data-stream) the-empty-stream)
(else (stream-cons (stream-ref data-stream
(stream-car order-stream))
(reorder (stream-cdr order-stream) data-stream)))))当我执行(reorder order-stream data-stream)时,我得到#stream作为输出,而不是6 . #promise。这是我的编程任务,所以不要给出完整的代码,而是给一些提示。
发布于 2015-10-15 12:18:36
看看你拥有的:
(define (reorder order-stream data-stream)
^^^^^^^^^^^^
(cond ((stream-null? order-stream) the-empty-stream)
((stream-null? data-stream) the-empty-stream)
(else (stream-cons (stream-ref data-stream
(stream-car order-stream))
(reorder (stream-cdr order-stream) data-stream)))))
^^^^^^^^^^^^^^^^^^^^^^^^^
(define (stream-cdr stream)
(force (cdr stream)))(stream-cdr stream)预期会返回什么样的东西?
然后你给我打电话
(define order-stream (cons-stream 2 1))order-stream的cdr是什么?
流编程很有趣。例如,要计算流p-s [a,b,c...] = a, a+b, a+b+c, ...的部分和,我们可以编写
(define (partial-sums xs init)
(cons-stream init
(partial-sums (stream-cdr xs)
(+ (stream-car xs) init))))我们可以轻松地将+抽象为一个通用的二进制操作参数:
(define (scanl + xs init)
(cons-stream init
(scanl + (stream-cdr xs)
(+ (stream-car xs) init))))并称之为.
(define factorials (scanl * integers 1))另一种定义scanl的有趣方法是
(define (scanlist + xs init)
(define rs (cons-stream init
(combine-streams + xs rs)))
rs)编写combine-streams (a.k.a.zipWith是一笔直截了当的交易。
在Racket中,一定要使用完整的语言,其中define是递归的,类似于letrec (而不是let),否则最后一个语言就不能工作了(您知道为什么吗?)
发布于 2015-10-14 07:46:24
球拍有一个内置的stream-cons,你无意中调用了它,而不是你打算使用的cons-stream。
https://stackoverflow.com/questions/33118736
复制相似问题