Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 923853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:15:28+00:00 2026-05-15T19:15:28+00:00

I like Clojure. One thing that bothers me about the language is that I

  • 0

I like Clojure. One thing that bothers me about the language is that I don’t know how lazy sequences are implemented, or how they work.

I know that lazy sequences only evaluate the items in the sequence that are asked for. How does it do this?

  • What makes lazy sequences so efficient that they don’t consume much
    stack?
  • How come you can wrap recursive calls in a lazy sequence and no
    longer get a stack over flow for large computations?
  • What resources do lazy sequences consume to do what it does?
  • In what scenarios are lazy sequences inefficient?
  • In what scenarios are lazy sequences most efficient?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:15:29+00:00Added an answer on May 15, 2026 at 7:15 pm

    Let’s do this.

    • I know that lazy sequences only evaluate the items in the sequence that are asked for, how does it do this?

    Lazy sequences (henceforth LS, because I am a LP, or Lazy Person) are composed of parts. The head, or the part(s, as really 32 elements are evaluated at a time, as of Clojure 1.1, and I think 1.2) of the sequence that have been evaluated, is followed by something called a thunk, which is basically a chunk of information (think of it as the rest of the your function that creates the sequence, unevaluated) waiting to be called. When it is called, the thunk evaluates however much is asked of it, and a new thunk is created, with context as necessary (how much has been called already, so it can resume from where it was before).

    So you (take 10 (whole-numbers)) – assume whole-numbers is a lazy sequence of whole numbers. That means you’re forcing evaluation of thunks 10 times (though internally this may be a little difference depending on optimizations.

    • What makes lazy sequences so efficient that they don’t consume much stack?

    This becomes clearer once you read the previous answer (I hope): unless you call for something in particular, nothing is evaluated. When you call for something, each element of the sequence can be evaluated individually, then discarded.

    If the sequence is not lazy, oftentimes it is holding onto its head, which consumes heap space. If it is lazy, it is computed, then discarded, as it is not required for subsequent computations.

    • How come you can wrap recursive calls in a lazy sequence and no longer get a stack over flow for large computations?

    See the previous answer and consider: the lazy-seq macro (from the documentation) will

    will invoke the body only the first time seq
    is called, and will cache the result and return it on all subsequent
    seq calls.
    

    Check out the filter function for a cool LS that uses recursion:

    (defn filter
      "Returns a lazy sequence of the items in coll for which
      (pred item) returns true. pred must be free of side-effects."
      [pred coll]
      (let [step (fn [p c]
                     (when-let [s (seq c)]
                       (if (p (first s))
                         (cons (first s) (filter p (rest s)))
                         (recur p (rest s)))))]
        (lazy-seq (step pred coll))))
    

    • What resources do lazy sequences consume to do what it does?

    I’m not quite sure what you’re asking here. LSs require memory and CPU cycles. They just don’t keep banging the stack, and filling it up with results of the computations required to get the sequence elements.

    • In what scenarios are lazy sequences inefficient?

    When you’re using small seqs that are fast to compute and won’t be used much, making it an LS is inefficient because it requires another couple chars to create.

    In all seriousness, unless you’re trying to make something extremely performant, LSs are the way to go.

    • In what scenarios are lazy sequences most efficient?

    When you’re dealing with seqs that are huge and you’re only using bits and pieces of them, that is when you get the most benefit from using them.

    Really, it’s pretty much always better to use LSs over non-LSs, in terms of convenience, ease of understanding (once you get the hang of them) and reasoning about your code, and speed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In clojure, I'd like to know what are the differences between the three below.
In clojure, I would like to write a tail-recursive function that memoizes its intermediate
I have a Clojure network application, basic structure is like this: Server has one
Context I like Clojure. I like Clojure more than I like ClojureScript. I like
I'm calling the twitter4j library using Clojure like so: (def twitter (. (TwitterFactory.) getInstance))
Is there a Clojurescript library which makes the DOM look like a Clojure data
Does clojure have a powerful 'loop' like common lisp. for example: get two elements
What I would like to do (in Clojure): For example, I have a vector
I'd like to call a Clojure function from Java code. This question hasn't been
When explaining Clojure builds, I would like to use the correct terminology. Therefore my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.