How to create a lazy-seq generating, anonymous recursive function in Clojure?

前端 未结 2 517
孤街浪徒
孤街浪徒 2021-02-07 17:57

Edit: I discovered a partial answer to my own question in the process of writing this, but I think it can easily be improved upon so I will post it anyway. Mayb

2条回答
  •  别跟我提以往
    2021-02-07 18:11

    fn takes an optional name argument with that name bound to the function in its body. Using this feature, you could write fibs as:

    (def fibs ((fn generator [a b] (lazy-seq (cons a (generator b (+ a b))))) 0 1))
    

提交回复
热议问题