For example, when defining the natural number sequence stream, I can use
;; -*- lexical-binding: t; -*-
(defun nats (n)
(cons n (lambda () (nats (1+ n)))))
(nats 0)
=> (0 closure ((n . 0) t) nil (nats (1+ n)))
But I want to call simply (nats)
since the natural numbers begins with 0
, and I don't want define a global helper function for nats
since it is useless everywhere else. I tried the following, but it doesn't work.
;; -*- lexical-binding: t; -*-
(defalias 'nats
(flet ((f (n) (cons n (lambda () (f (1+ n))))))
(lambda () (f 0))))
(nats)
error--> nats: Symbol's function definition is void: f
It looks like f
is not in the closure's environment. I also tried some other options. cl-letf
produces the same result. cl-flet
doesn't allow recursive local function definition.