gpt4 book ai didi

lisp - Scheme (Lazy Racket) 自然数的无限列表

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

我认为 Lazy Racket应该对处理无限列表有用。根据Wikipedia Lazy Racket article , fibs(斐波那契数列的无限列表)可以定义为:

;; An infinite list:
(define fibs (list* 1 1 (map + fibs (cdr fibs))))

我们如何定义无限的自然数列表?

最佳答案

#lang lazy
(define Nat (cons 1 (map (lambda (x) (+ x 1)) Nat)))

感谢 Will Ness。

我也找到了

#lang lazy
;;; infinite sequences represented by a_(n+1) = f(a_n)
(define inf-seq (lambda (a0 f) (cons a0 (inf-seq (f a0) f))))
(define Nat (inf-seq 1 (lambda (x) (+ x 1))))

输出

(define (outputListData list)
(cond
[(null? list) #f] ; actually doesn't really matter what we return
[else
(printf "~s\n" (first list)) ; display the first item ...
(outputListData (rest list))] ; and start over with the rest
)
)

(outputListData Nat)

关于lisp - Scheme (Lazy Racket) 自然数的无限列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17258211/

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