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

标签 lisp scheme racket lazy-evaluation infinite

我认为 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/

相关文章:

list - 获取列表中的第 N 个元素作为列表而不是 LISP 中的数字?

lisp - Common Lisp 中的可参数化返回

emacs - Option 或 Command 键作为 Macintosh 上 LispBox 的元键

reference - 在 Common Lisp 中,何时引用对象以及何时直接按值访问对象?

javascript - 内联执行生成的汇编程序

coding-style - 方案风格指南

function - 函数可以返回不是 lambda 的函数吗?

macros - Racket:由定义为macroname.0的宏生成的宏

racket - Racket 和花栗鼠的 FFI 问题

scheme - 你如何在 Scheme 中返回一个过程的描述?