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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:39:53+00:00 2026-06-03T06:39:53+00:00

How do you split a list into halves using list comprehension? e.g. If I

  • 0

How do you split a list into halves using list comprehension?

e.g. If I have [1,1,2,2,3,3,4,4,5,5] and I only want [1,1,2,2,3]

my attempts so far:

half mylist = [r | mylist!r ; r <- [0..(#mylist div 2)] ]    ||does not work

Any thoughts?

[Nb: This isn’t actually Haskell but similar. ! is used for indexing list, and # gives length)

Edit::

Okay so it turns out that

half mylist = [r | r <- [mylist!0..mylist!(#mylist div 2)] ]

works, but only in list of numbers and not strings. Any clues?

  • 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-03T06:39:54+00:00Added an answer on June 3, 2026 at 6:39 am

    This isn’t really an appropriate thing to do with a list comprehension. List comprehensions are alternate syntax for maps and filters (and zips). Splitting a list is a fold.

    As such, you should consider a different approach. E.g.

    halve :: [a] -> [a]
    halve [] = []
    halve xs = take (n `div` 2) xs
        where n = length xs
    

    Splitting isn’t a great operation on large lists, since you take the length first (so it is always n + n/2 operations on the list. It is more appropriate for array-like types that have O(1) length and split.

    • 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 that I want to split into individual lists
I want to split my list into rows that have all the same number
I need to split a list into two equal lists. For Example: I have
My goal is to convert list into array using split method then remove element(depends)
I have a list of items that I want to split based on a
I want to split List of user generic List into its small list with
The problem is to split a list of items into a variable number of
I'm seeking an algorithm to split a list of items of varying sizes into
I'm trying to split a string into a int list for further processing. But
Possible Duplicate: How do you split a list into evenly sized chunks in Python?

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.