lazy-seq StackOverflowError

Matteo

Why does this work

(def fibs (cons 0 (cons 1 (lazy-seq (map + fibs (rest fibs))))))
(take 10 fibs)

while this other

(def fibs (lazy-seq (cons 0 (cons 1 (map + fibs (rest fibs))))))
(take 10 fibs)

generate a StackOverflowError?

ClojureMostly

Let's first note that it also works with one cons outside:

(def fibs (cons 0 (lazy-seq (cons 1 (map + fibs (rest fibs))))))

Let's also establish that lazy-seq is a macro (obviously, since it doesn't evaluate the arguments) and puts the body (the argument(s) to lazy-seq) into a function.

Now, if you "request" an element from that sequence that function will be called once and subsequent calls will return the cached value.

Obviously, only once that function returns something you have a value available. Now, what's happening in our bad case:

Imagine clojure is in the middle of evaluating the function for the very first time. The first thing it needs is +, fibs and (rest fibs) in order to pass this to cons. Now clojure will see that fibs is a lazy sequence and invoke it! This is because you're currently in your first invocation and haven't returned. This is causing an infinite recursion.

The fix is easy: Make sure there is a realized element in the beginning of the list so that the two expressions, fibs and (rest fibs) can return something. In particular, taking the rest of (cons 1 whatever) will return whatever without realizing a single element (important since otherwise we'd have the same problem again).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

clojure: create a lazy-seq containing another lazy-seq

From Dev

Custom implementation of count for lazy seq

From Dev

Generate lazy seq with recursion in Clojure?

From Dev

read lazy seq - how to add the index

From Dev

Clojure's lazy-seq caching

From Dev

lazy-seq and stack overflow for infinite sequences

From Dev

Get all realized values from a lazy seq

From Dev

Get all realized values from a lazy seq

From Dev

Clojure read the content of a lazy-seq ({ })

From Dev

Create a Clojure lazy seq from another lazy seq, combining some elements, removing others

From Dev

Does using `lazy-seq` recursively take up stack frames?

From Dev

Does using `lazy-seq` recursively take up stack frames?

From Dev

Data.Sequence.Seq lazy parallel Functor instance

From Dev

F#'s list is eager or lazy evaluated? Or only "seq" is lazy evaluated in F#

From Dev

If the only non-stack-consuming looping construct in Clojure is "recur", how does this lazy-seq work?

From Dev

How to create a lazy-seq that repeats elements of another collection, in random order?

From Dev

If the only non-stack-consuming looping construct in Clojure is "recur", how does this lazy-seq work?

From Dev

StackOverflowError with @EmbeddedId

From Dev

Gson : StackOverflowError

From Dev

What is StackOverflowError?

From Dev

Pathfinding - StackOverflowError

From Dev

Scala Parallel Seq not confroming to Seq

From Dev

Transform Seq[A] to Map[Int, Seq[A]]

From Dev

Scala flatten a Seq[Future[Seq[]]]

From Dev

StackOverflowError building a tree

From Dev

Occasional StackOverflowError in Recursive Calculation

From Dev

Is it safe to catch StackOverflowError in Java?

From Dev

Getting FontMetrics StackOverflowError

From Dev

Avoid StackOverflowError in clojure with reduce

Related Related

  1. 1

    clojure: create a lazy-seq containing another lazy-seq

  2. 2

    Custom implementation of count for lazy seq

  3. 3

    Generate lazy seq with recursion in Clojure?

  4. 4

    read lazy seq - how to add the index

  5. 5

    Clojure's lazy-seq caching

  6. 6

    lazy-seq and stack overflow for infinite sequences

  7. 7

    Get all realized values from a lazy seq

  8. 8

    Get all realized values from a lazy seq

  9. 9

    Clojure read the content of a lazy-seq ({ })

  10. 10

    Create a Clojure lazy seq from another lazy seq, combining some elements, removing others

  11. 11

    Does using `lazy-seq` recursively take up stack frames?

  12. 12

    Does using `lazy-seq` recursively take up stack frames?

  13. 13

    Data.Sequence.Seq lazy parallel Functor instance

  14. 14

    F#'s list is eager or lazy evaluated? Or only "seq" is lazy evaluated in F#

  15. 15

    If the only non-stack-consuming looping construct in Clojure is "recur", how does this lazy-seq work?

  16. 16

    How to create a lazy-seq that repeats elements of another collection, in random order?

  17. 17

    If the only non-stack-consuming looping construct in Clojure is "recur", how does this lazy-seq work?

  18. 18

    StackOverflowError with @EmbeddedId

  19. 19

    Gson : StackOverflowError

  20. 20

    What is StackOverflowError?

  21. 21

    Pathfinding - StackOverflowError

  22. 22

    Scala Parallel Seq not confroming to Seq

  23. 23

    Transform Seq[A] to Map[Int, Seq[A]]

  24. 24

    Scala flatten a Seq[Future[Seq[]]]

  25. 25

    StackOverflowError building a tree

  26. 26

    Occasional StackOverflowError in Recursive Calculation

  27. 27

    Is it safe to catch StackOverflowError in Java?

  28. 28

    Getting FontMetrics StackOverflowError

  29. 29

    Avoid StackOverflowError in clojure with reduce

HotTag

Archive