(define fibs
(stream-cons 0
(stream-cons 1
(stream-add (stream-cdr fibs) fibs))))
The code above is an excerpt from
SICP that generates an infinite Fibonacci sequence. I've tried to migrate it to Clojure.
The following code is the implementations of the stream concept mentioned in
SICP and the the Fibonacci function. The essential part is this: (defmacro stream-cons [e s] ...). Because Clojure's cons requires that s to be a ISeq or Seqable, that makes the "(stream-cdr fibs)" part always fails. Because this piece of code is executed before the outer stream-cons ever happened. By the time we execute "(stream-cdr fibs)", we haven't had the value of "fibs" yet.
(defn pair [x y] (cons x [y]))
(defn pair-car [p] (first p))
(defn pair-cdr [p] (second p))
(defmacro stream-cons [e s]
`(pair ~e (delay ~s)))
(defn stream-car [s]
(pair-car s))
(defn stream-cdr [s]
(force (pair-cdr s)))
(defn stream-map
([f s]
(stream-cons
(f (stream-car s))
(stream-map f (stream-cdr s))))
([f s & ss]
(let [step (fn step [ss]
(when (not (empty? ss))
(stream-cons (map stream-car ss) (step (map stream-cdr ss)))))]
(stream-map #(apply f %) (step (cons s ss))))))
(defn stream-add [& ss]
(apply stream-map + ss))
(defn take-stream [s count]
(when (> count 0)
(stream-cons
(stream-car s)
(take-stream (stream-cdr s) (dec count)))))
(defn print-stream [s]
(let [step (fn step [s]
(when (not (empty? s))
(print (stream-car s) "")
(recur (stream-cdr s))))]
(step s)
(println)))
(def fibs
(stream-cons 0
(stream-cons 1
(stream-add (stream-cdr fibs) fibs))))
(def ones (stream-cons 1 ones))
(def twos (stream-add ones ones))
(print-stream (take-stream twos 10)) ; => 2 2 2 2 2 2 2 2 2 2
(print-stream (take-stream fibs 10)) ; => 0 1 1 2 3 5 8 13 21 34
Interesting enough that I haven't thought of a way to seamlessly integrate the above stream code into Clojure. If we implement it using (deftype LazyStream [] clojure.lang.Seq ...) that would make cons a
function instead of a macro. Therefore I temporarily think that it's impossible to seamlessly integrate the implementation.
分享到:
相关推荐
Clojure in Action 文字版pdf
Learn to handle data using sequences, reducers, and transducers in Clojure Explore the lesser known and more advanced features, constructs, and methodologies of the Clojure language and its ecosystem,...
Clojure is an opinionated language—it doesn’t try to cover...Hickey had in mind while designing Clojure and some of the more specific decisions that are built into the language to support these goals.
Clojure has the power inherent in Lisp, but is not constrained by the history of Lisp. Clojure is a functional language. Data structures are immutable, and functions tend to be side-effect free. ...
Designed specifically to meet the needs of professional developers, this book briefly introduces functional programming before skipping directly to the heart of using Clojure in a real-world setting....
【1】[Clojure编程乐趣](The Joy of Clojure).pdf 【2】Clojure – Functional Programming for the JVM中文版.pdf ...【6】Clojure in Action.pdf 【7】clojure in small pieces.pdf 以上7本书的电子版
[2013] Functional Programming Patterns in Scala and Clojure - Write Lean Programs for the JVM.(Michael Bevilacqua-Linn).[1937785475].pdf+epub.rar [2014] Clojure Cookbook - Recipes for Functional ...
The book details a large number of recipes – pairs of problems and solutions – for common topics in Clojure. Clojure Cookbook doesn't just teach you Clojure, it also shows you how to use the ...
Like most general purpose languages, various Clojure features have different performance characteristics that one should know in order to write high performance code. This book shows you how to ...
Practical Clojure Clojure语言书籍
clojure clojure clojureclojure clojure
[2011] Clojure in Action.(Amit Rathore).[1935182595].pdf [2011] The Joy of Clojure - Thinking the Clojure Way.(Michael Fogus, Chris Houser).[1935182641].pdf [2012] Clojure Programming - Practical Lisp...
It also describes how you can implement these machine learning techniques in Clojure. The book also demonstrates several Clojure libraries, which can be useful in solving machine learning problems.
The Joy of Clojure, Second Edition is a deep look at the Clojure language. Fully updated for Clojure 1.6, this new edition goes beyond just syntax to show you the "why" of Clojure and how to write ...
Typed Clojure 保留了 Clojure 的优势,是 Clojure 的可选类型系统,也可以说是 Clojure 的一个库,改善了大量的静态类型安全检测。主要特性:从 Java 中保护你的 Clojure 程序,进行安全的互操作,正确的使用外部 ...
25 recipes to deeply understand and implement advanced algorithms in Clojure About This Book Explore various advanced algorithms and learn how they are used to address many real-world computing ...
Clojure编程乐趣和clojure_programming.pdf两本书
[2015] Living Clojure.(Carin Meier).[1491909048].pdf [2016] Clojure for Finance.(Timothy Washington).[1785289284].pdf [2016] Clojure for Java Developers.(Eduardo Diaz).[178528150X].pdf [2016] Clojure...