5 lisp scheme infinite lazy-evaluation racket
我认为Lazy Racket应该对处理无限列表很有用.根据Wikipedia Lazy Racket的文章,fibs(Fibonacci数字的无限列表)可以定义为:
;; An infinite list:
(define fibs (list* 1 1 (map + fibs (cdr fibs))))
Run Code Online (Sandbox Code Playgroud)
我们如何定义无限的自然数列表?
小智 3
#lang lazy
(define Nat (cons 1 (map (lambda (x) (+ x 1)) Nat)))
Run Code Online (Sandbox Code Playgroud)
感谢威尔·尼斯。
我还发现
#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))))
Run Code Online (Sandbox Code Playgroud)
输出
(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)
Run Code Online (Sandbox Code Playgroud)