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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:07:48+00:00 2026-05-29T05:07:48+00:00

I have a list of integers, I need to append elements to it at

  • 0

I have a list of integers, I need to append elements to it at different times.

let xs =[]::[Int]

usually appending elements to this would be like:

1:xs

but when using IO in a function, it doesn’t seem to work within a do block and gives errors, and

let xs = [1]
let xs=(2:xs)

results in an infinite list like [1,2,2,2,2,…..]
What can I do to correct this?

  • 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-29T05:07:49+00:00Added an answer on May 29, 2026 at 5:07 am

    You seem to have a fundamental misunderstanding about lists in Haskell. The lists are always immutable, so there is no way to add new elements to an existing list. I.e. You can only create new lists.

    So, accordingly, the a:b operator never adds an element to a list, but creates a new list where a is the first element, which is followed by the existing list b.

    When you say:

    let xs = 2 : xs
    

    You are saying that xs is a list where the first element is 2 and the rest of the list is xs itself, which logically results in an infinite list of 2’s. In the context of this question, it’s irrelevant whether you are in the IO monad or not.

    So given the above, you need to do something like

    let xs1 = [1]
    let xs2 = 2:xs1
    let xs3 = 3:xs2
    

    But of course, this is the same as simply doing

    let xs3 = [3,2,1]
    

    So you really need to give some more context on what kind of list you want to build and why.

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

Sidebar

Related Questions

i have a list of elements (let's say integers), and i need to make
I have a list of elements (integers) and what I need to do is
I have a list of integers or of strings and need to pass it
I have the following List<int> collection and I need to find the highest integer
I have a list of integers like 1, 2, 3, 4, 5, 6, 7,
Say I have a list of integers: List<int> myInts = new List<int>() {1,2,3,5,8,13,21}; I
I have a list containing a tuples and long integers the list looks like
I have a list of integers which I need to parse into a string
I have a list of integers and I need to count how many of
Say you need to have a list/array of integers which you need iterate frequently,

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.