`
yangdong
  • 浏览: 65022 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Stream in Clojure

阅读更多
(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.
分享到:
评论
2 楼 yangdong 2011-01-10  
谢谢。我只是追求形似,所以找不到对应的写法。
1 楼 jamesqiu 2011-01-10  
Clojure的lazy-cat和Scala的Stream实现fib数列都很清楚明白:
--- Clojure
(defn fib [a b] (lazy-cat [a] (fib b (+ a b))))
(take 10 (fib 0 1))
--- Sclaa
def fib(a:Int,b:Int):Stream[Int] = a #:: fib(b,a+b)
fib(0,1) take 10 toList

相关推荐

    Clojure in Action

    Clojure in Action 文字版pdf

    Mastering.Clojure.1785

    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编程乐趣

    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.

    Programming Clojure 英文电子版

    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. ...

    Professional.Clojure.1119267277

    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....

    clojure相关书籍1

    【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本书的电子版

    Clojure电子书合集2(13本)

    [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 ...

    Clojure Cookbook

    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 ...

    Clojure High Performance Programming(PACKT,2ed,2015)

    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.pdf

    Practical Clojure Clojure语言书籍

    clojure eclipse

    clojure clojure clojureclojure clojure

    Clojure电子书合集1(12本)

    [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...

    Clojure for Machine Learning(PACKT,2014)

    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, 2nd Edition

    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 ...

    Clojure可选类型系统TypedClojure.zip

    Typed Clojure 保留了 Clojure 的优势,是 Clojure 的可选类型系统,也可以说是 Clojure 的一个库,改善了大量的静态类型安全检测。主要特性:从 Java 中保护你的 Clojure 程序,进行安全的互操作,正确的使用外部 ...

    Clojure.Data.Structures.and.Algorithms.Cookbook.1785281453

    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

    Clojure编程乐趣和clojure_programming.pdf两本书

    Clojure电子书合集_12本3

    [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...

Global site tag (gtag.js) - Google Analytics