Skip to content

Commit f8da768

Browse files
committed
lazyFibs animation
1 parent e80e68e commit f8da768

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Gemfile.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ GEM
1111
eventmachine (1.2.7)
1212
ffi (1.17.0-arm64-darwin)
1313
ffi (1.17.0-x64-mingw-ucrt)
14-
ffi (1.17.0-x86_64-linux-gnu)
14+
ffi (1.17.0-x86_64-linux)
1515
forwardable-extended (2.6.0)
1616
http_parser.rb (0.8.0)
1717
i18n (1.14.5)
@@ -64,7 +64,7 @@ GEM
6464
racc (~> 1.4)
6565
nokogiri (1.18.9-x64-mingw-ucrt)
6666
racc (~> 1.4)
67-
nokogiri (1.18.9-x86_64-linux-gnu)
67+
nokogiri (1.18.9-x86_64-linux)
6868
racc (~> 1.4)
6969
pathutil (0.16.2)
7070
forwardable-extended (~> 2.6)
@@ -86,6 +86,7 @@ GEM
8686
tzinfo-data (1.2024.1)
8787
tzinfo (>= 1.0.0)
8888
unicode-display_width (2.5.0)
89+
wdm (0.1.1)
8990
webrick (1.8.2)
9091

9192
PLATFORMS
@@ -102,6 +103,7 @@ DEPENDENCIES
102103
minima
103104
nokogiri
104105
tzinfo-data
106+
wdm (~> 0.1.0)
105107
webrick
106108

107109
BUNDLED WITH

_chapters/haskell1.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,21 @@ fibs n = case n of
405405

406406
</div>
407407

408+
## A lazy infinite sequence
409+
410+
Let's briefly revisit fibonacci numbers and demonstrate a very idiomatic haskell construction for defining a lazy sequence. In the following definition for `lazyFibs`, `zipWith` is a function which uses the specified function (in this case `(+)`) to pair the heads of two given lists. In this case, we are zipping over recursive references to `lazyFibs` and `tail lazyFibs`.
411+
412+
```haskell
413+
lazyFibs = 1 : 1 : zipWith (+) lazyFibs (tail lazyFibs)
414+
```
415+
We can then create as much of the list as we need:
416+
```haskell
417+
take 10 lazyFibs
418+
[1,1,2,3,5,8,13,21,34,55]
419+
```
420+
This is only possible because Haskell's lazy evaluation only forces evaluation of the heads of the lists as necessary, e.g. to output the result of `take 10`.
421+
![Deck Observable Visualised](/assets/images/chapterImages/haskell1/zip.gif)
422+
408423
## Glossary
409424

410425
*GHCi REPL*: The interactive Read-Eval-Print Loop for GHC, the Glasgow Haskell Compiler, allowing users to test Haskell programs and expressions interactively.
410 KB
Loading

0 commit comments

Comments
 (0)