我无法弄清楚为什么这种懒惰的序列不打印。我已经看过这里的其他帖子(例如(,似乎没有什么是我想要的。这是代码段...
(defn exp [x n]
(reduce * (repeat n x))
)
(defn primes
([] (primes 1 1))
([n m] (if (= n 1) (lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (+ m 1) (1))))
(lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (- n 1) (+ m 1)))))
)
)
(take 4 (primes))
任何帮助将不胜感激。谢谢!
这三个评论确实给出了答案。但实际上查看代码总是很好,因此有两种提供印刷输出的方法。
(defn exp [x n]
(reduce * (repeat n x)))
(defn primes
([] (primes 1 1))
([n m] (if (= n 1)
(lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (+ m 1) 1)))
(lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (- n 1) (+ m 1)))))))
(defn x-1 []
(doseq [prime (take 4 (primes 2 2))]
(println prime)))
(defn x-2 []
(->> (primes 2 2)
(take 4)
(apply println)))