我将如何实现重复功能,该功能将使流程充满重复INT?

发布于 2025-01-25 11:03:50 字数 1326 浏览 3 评论 0原文

问题是:编写一个函数(重复X),该函数在给定值x的情况下会产生一个重复x ad infinitum的流。例如,呼叫(取(重复42)5)产生(42 42 42 42 42 42)。

我的问题是我的重复功能不知道该停止什么...因此它永远运行。

这是我到目前为止的代码。

#lang R5RS
;; Repeat your solution for take
(define (integer-stream x)
    (cons x  (delay (integer-stream (+ 1 x)))))
(define nat (integer-stream 0))

(define (stream-car stream)
  (car stream))

(define (stream-cdr stream)
  (force (cdr stream)))

(define (head stream)  (car stream))
(define (rest stream)  (force (cdr stream)))

(define (take s k)
  (cond ((= k 0) '())
        (else (cons (car s) (take (stream-cdr s) (- k 1))))))

;;;;;;;;;
(define (stream-map f s)
  (if (null? s) '()
      (cons (f (car s))
            (delay (stream-map f (rest s))))))

(define (squares x)
  (* x x))

(define (square s)
  (cond ((null? s) '())
        (else (stream-map squares s))))

;;;;;;;;;;;
(define (cubess x)
  (* x x x))

(define (cube s)
  (cond ((null? s) '())
        (else (stream-map cubess s))))

;;;;
(define (merge-streams s t)
  (cond ((null? s) t)
        ((null? t) s)
        ((< (stream-car s) (stream-car t))
         (cons (stream-car s)
               (delay (merge-streams (stream-cdr s) t))))
        (else (cons (stream-car t) (delay (merge-streams s (stream-cdr t)))))))

;;;;;

(define (repeat x)
  (cons x (repeat x)))

The questions is: Write a function (repeat x) which, given a value x, produces a stream that repeats x ad infinitum. For instance, the call (take (repeat 42) 5) produces (42 42 42 42 42).

My problem is that my repeat function doesn't know what to stop... so it runs forever.

This is the code I have so far.

#lang R5RS
;; Repeat your solution for take
(define (integer-stream x)
    (cons x  (delay (integer-stream (+ 1 x)))))
(define nat (integer-stream 0))

(define (stream-car stream)
  (car stream))

(define (stream-cdr stream)
  (force (cdr stream)))

(define (head stream)  (car stream))
(define (rest stream)  (force (cdr stream)))

(define (take s k)
  (cond ((= k 0) '())
        (else (cons (car s) (take (stream-cdr s) (- k 1))))))

;;;;;;;;;
(define (stream-map f s)
  (if (null? s) '()
      (cons (f (car s))
            (delay (stream-map f (rest s))))))

(define (squares x)
  (* x x))

(define (square s)
  (cond ((null? s) '())
        (else (stream-map squares s))))

;;;;;;;;;;;
(define (cubess x)
  (* x x x))

(define (cube s)
  (cond ((null? s) '())
        (else (stream-map cubess s))))

;;;;
(define (merge-streams s t)
  (cond ((null? s) t)
        ((null? t) s)
        ((< (stream-car s) (stream-car t))
         (cons (stream-car s)
               (delay (merge-streams (stream-cdr s) t))))
        (else (cons (stream-car t) (delay (merge-streams s (stream-cdr t)))))))

;;;;;

(define (repeat x)
  (cons x (repeat x)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑看君怀她人 2025-02-01 11:03:50

您很接近 - 您只需要使用 delay 在您的中重复函数:

(define (repeat arg)
  (cons arg (delay (repeat arg))))

其余代码应相同 - 因此,这将是完整的代码:

#lang r5rs

(define (stream-car stream)
  (car stream))

(define (stream-cdr stream)
  (force (cdr stream)))

(define (take stream n)
  (if (zero? n) '()
      (cons (stream-car stream)
            (take (stream-cdr stream) (- n 1))))) 

(define (repeat arg)
  (cons arg (delay (repeat arg))))

test:test:

> (repeat 42)
(42 . #<promise>)

> (take (repeat 42) 5)
(42 42 42 42 42)

You're close- you just have to use delay in your repeat function:

(define (repeat arg)
  (cons arg (delay (repeat arg))))

Rest of code should be the same- so this will be the full code:

#lang r5rs

(define (stream-car stream)
  (car stream))

(define (stream-cdr stream)
  (force (cdr stream)))

(define (take stream n)
  (if (zero? n) '()
      (cons (stream-car stream)
            (take (stream-cdr stream) (- n 1))))) 

(define (repeat arg)
  (cons arg (delay (repeat arg))))

Test:

> (repeat 42)
(42 . #<promise>)

> (take (repeat 42) 5)
(42 42 42 42 42)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文