简体   繁体   English

球拍流

[英]streams in racket

Can anyone help me better understand how to write a stream? 任何人都可以帮助我更好地了解如何编写流吗?

I understand that a stream is an infinite sequence of values and the way I have learned programming them is a representing them as a thunk that when called produces a pair of (1) the first element in the sequence and (2) a thunk that represents the stream for the second-through-infinity elements 我理解一个流是一个无限的值序列,我学会编程它们的方式是将它们表示为一个thunk,当被调用时产生一对(1)序列中的第一个元素和(2)代表的thunk第二个无限元素的流

For example: 例如:

(define powers-of-two
    (letrec ([f (lambda (x) (cons x (lambda () (f (* x 2)))))])
        (lambda () (f 2))))

I understand here that it is just producing a powers of two and to access these for example calling (car (powers-of-two)) would result in 2 and calling (car ((cdr (powers-of-two)))) would result in 4 我在这里理解它只是产生2的幂并且访问这些例如调用(car (powers-of-two))将导致2并且调用(car ((cdr (powers-of-two))))会导致4

Now I am trying to write a stream called red-blue that alternates between strings red and blue but I am a little confused about how to construct it 现在我正在尝试编写一个名为red-blue的流,它在redblue字符串之间交替,但我对如何构建它有点困惑

It looks like you were asking how to build your own custom streams with thunks, which others have already answered. 看起来你问的是如何使用thunks构建自己的自定义流,其他人已经回答过。 Just in case, it's worth noting that Racket has a stream library built-in and most Racketeers would use that for streams. 为了以防万一,值得注意的是,Racket内置了一个流库,大多数Racketeers会将其用于流。

Here's an example: 这是一个例子:

#lang racket
(require racket/stream)
(define reds (stream-cons "red" reds))
(define red-blues (stream-add-between reds "blue"))

;; show ten of them
(for ([i 10] [e (in-stream red-blues)])
  (displayln e))

For a general understanding of streams in Scheme, I'd recommend section §3.5 Streams in the SICP book. 对于方案中的数据流的一个大致的了解,我建议部分3.5节流在SICP书。 It'll teach you the basic concepts to solve stream-related problems such as the one in the question. 它将教你解决与流相关的问题的基本概念,例如问题中的问题。

Regarding the problem in the question, here's the general idea to solve it: 关于问题中的问题,这是解决问题的一般想法:

  • Build two infinite streams, one producing only the string "red" and the other "blue" 构建两个无限流,一个只生成字符串"red" ,另一个生成"blue"
  • Combine both streams taking one element from one and then one element from the other (alternating), this procedure is called interleave in SICP 将两个流合并为一个元素,然后从另一个元素中取出一个元素(交替),此过程在SICP中称为interleave

I wrote SRFI-41 which describes streams, provides an implementation, and gives many examples. 我写了SRFI-41 ,它描述了流,提供了一个实现,并给出了很多例子。 The streams there differ from those in SICP, and are "better" in a way explained in the SRFI. 那里的流与SICP中的流不同,并且以SRFI中解释的方式“更好”。

I am a newbie at this but the following solutions seems to work as well: 我是新手,但以下解决方案似乎也有效:

 (define red-then-blue
   (letrec ([f (lambda (x) (cons x(lambda ()(f(cond [(string=? x "red") "blue"]
                                              ["red"])))))])
   (lambda () (f "red"))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM