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 8535015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:19:50+00:00 2026-06-11T10:19:50+00:00

Running this works as expected: (defn long-seq [n] (lazy-seq (cons (list n {:somekey (*

  • 0

Running this works as expected:

(defn long-seq [n]
  (lazy-seq (cons 
             (list n {:somekey (* n 2)})
             (long-seq (+ n 1)))))
(take 3 (long-seq 3))
; => ((3 {:somekey 6}) (4 {:somekey 8}) (5 {:somekey 10}))

However I would like to do the same thing with a vector:

(defn long-seq-vec [n]
  (lazy-seq (into 
             (vector (list n {:somekey (* n 2)}))
             (long-seq-vec (+ n 1)))))
(take 3 (long-seq-vec 3))

This gives me a stack overflow. Why?

  • 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-11T10:19:52+00:00Added an answer on June 11, 2026 at 10:19 am

    The main reason is that vectors aren’t lazy – so the into call greedily consumes the recursive sequences produced by long-seq-vec and results in a stack overflow. As a corollary of this, it’s not possible to create an infinite vector (in general, you can only create an infinite data structure if it is lazy or cyclic).

    It works in the first example because cons is quite happy to behave lazily when consing onto the front of a lazy sequence, so the sequence can be infinite.

    Assuming you actually want an infinite sequence of vectors I’d suggest something like:

    (defn long-seq-vec [n]
      (lazy-seq (cons 
                  (vector n {:somekey (* n 2)})
                  (long-seq-vec (+ n 1)))))
    
    (take 3 (long-seq-vec 3))
    
    => ([3 {:somekey 6}] [4 {:somekey 8}] [5 {:somekey 10}])
    

    Or as an alternative, you can use for which is lazy in itself:

    (defn long-seq-vec [n]
      (for [x (iterate inc n)]
        (vector x {:somekey (* x 2)})))
    

    I prefer this as it avoids the lazy-seq/cons boilerplate, avoids recursion and is slightly more clear in expressing what your function does… it’s a little more “declarative” if you like. You could also use map in a similar way.

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

Sidebar

Related Questions

When running in quirks mode, this works as expected however when I declare HTML
I have problems running a dynamic LIKE statement in my project: this query works
This works on the iPhone BTW (Both running iOS 5.1) My app freezes when
This MySQL script installs multiple triggers. It works on one machine running MySQL 5.0.51b-community.
Running this query takes a long time: SELECT host,ip FROM arecords WHERE ip IN
OK, so I can run a command like this to get a list of
On my development machine, this works as expected (requests to server.com/.myhandler are executed as
I have a method that works in mysterious ways. Running this works fine; FindConcept(Product);
I've read numerous similar questions, but the answers simply didn't work. Running this function
I'm running Mysql 5.0.77 and I'm pretty sure this query should work? SELECT *

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.