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

  • SEARCH
  • Home
  • 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 8600577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:38:53+00:00 2026-06-12T01:38:53+00:00

Not sure what is the behaviour I observe while using flatten when constructing a

  • 0

Not sure what is the behaviour I observe while using flatten when constructing a lazy sequence.

Looking at the source in clojure.core I can see that the flatten function makes a call to filter and hence should return a lazy sequence – I think. And yet the following snippet gives me a stackoverflow error. In the snippet when the call to flatten is replaced with a call to concat, it works just fine

(defn l-f [c]
  (if (nil? c) []
    (lazy-seq  (flatten (cons  [[ :h :j] :a :B] (l-f (rest c))))))) 


    (take 10 (l-f (repeat 2))) is how I invoke it.

This is a rather contrived example. Also I am aware that flatten and concat will give me sequences where the nesting levels are different.

I am trying to figure out why flatten seems to break the laziness, even though my (limited) understanding of the code in clojure.core suggests otherwise.

  • 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-06-12T01:38:54+00:00Added an answer on June 12, 2026 at 1:38 am

    Laziness only takes you so far – laziness just means that the sequence isn’t fully realized at the time that it’s created, but building one lazy sequence from another sometimes involves looking ahead a few values. In this case, the implementation of flatten doesn’t play nicely with the recursive way in which you’re calling it.

    First, the flatten function calls tree-seq to do a depth-first traversal of the contents of the collection. In turn, tree-seq calls mapcat with the provided sequence, which delegates to apply, which realizes the first few items in the sequence to determine the arity of the function to invoke. Realizing the first few items in the sequence causes a recursive call to l-f, which calls flatten on the remaining arguments, and gets stuck in an infinite loop.

    In this particular situation, there’s no need to call flatten recursively, because any call after the first will have no effect. So your function can be fixed by separating out the generation of the lazy sequence from the flattening of it:

    (defn l-f [c]                                                      
      (letfn [(l-f-seq [x] (if-let [s (seq x)]                         
                             (lazy-seq (cons [[:h :j] :a :B] (l-f-seq (rest s))))
                             []))]                                               
        (flatten (l-f-seq c))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure this is a known issue. I’m using VS2012 RC (Ultimate), and Win8
As an example, imagine I have a table. The standard alignment behaviour (not sure
I am not sure if this is Java behaviour or rogue GWT behaviour. But
I would like to achieve the following behaviour, but I'm not sure how: User
I need to override the caret behaviour in a class, but I'm not sure
I have an IE bug that I'm not sure how to fix. Using jQuery
Im not sure if this behaviour is normal or not. Im hoping that my
Python is exhibiting a strange behaviour that I didnt witness before, not sure what
I'm not sure if this behaviour is intended, but it seems a bit weird
Can someone explain me the behaviour of the following line in VB Return Not

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.