grylix

grylix

Programming Clojure, Fourth Edition: lazy streams (p. 104)

On Page 104 you describe several ways to define lazy streams in general or the stream of the fibonacci numbers in general.

I propose to demonstrate the definition of streams as the fixed point of a given function which maps a stream to another stream. The given function must be a productive function, i.e. a function,
that can generate at least the beginning of a sequence.

(defn fixedpoint [f]
(letfn
[(stream [ ] (lazy-seq (f (stream))))]
(stream))
)

Let’s define some examples, e.g. the stream of zeros, ones, or the stream of alternating zero and ones:

(set! *print-length* 20)
(def zeros (fixedpoint (fn [s] (cons 0 s))))
(def ones (fixedpoint (fn [s] (cons 1 s))))
(def zeroone (fixedpoint (fn [s] (cons 0 (cons 1 s)))))

but more interesting is the definition of the stream of integers

(def ints (fixedpoint (fn [s] (cons 0 (map inc s)))))

and the stream of fibonacci numbers

(def fibs (fixedpoint (fn [s] (cons 0 (map + s (cons 1 s))))))

For me, this definition is very simple and clear.

Best wishes
Dominik

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
New
PragmaticBookshelf
Learn from the award-winning programming series that inspired the Elixir language, and go on a step-by-step journey through the most impo...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Sub Categories: