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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:34:00+00:00 2026-06-13T22:34:00+00:00

Ive got this code which takes an integer ‘n’, and a list, then splits

  • 0

Ive got this code which takes an integer ‘n’, and a list, then splits the list into ‘n’ lists.

chunk n xs = chunk' i xs
  where
    chunk' _ [] = []
    chunk' n xs = a : chunk' n b where (a,b) = splitAt n xs
    i = ceiling (fromIntegral (length xs) / fromIntegral n)

And this is an example of how it works:

*Main> chunk 5 [1..10]
[[1,2],[3,4],[5,6],[7,8],[9,10]]

Ive been trying to get this to work with the Data.ByteString library but cant figure it out.

This is the code ive been trying to use.

import qualified Data.ByteString as B
B.readFile "meow.txt" >>= (\x -> return $ chunk 4 x)

And this is the error that it gives me:

<interactive>:402:51:
    Couldn't match expected type `[a10]'
                with actual type `B.ByteString'
    In the second argument of `chunk', namely `x'
    In the second argument of `($)', namely `chunk 4 x'
    In the expression: return $ chunk 4 x

It appears to be a type mismatch problem, im assuming because of the fromIntegral. Is there any way to get the chunk function to accept byte strings?

My goal with this function is to strictly accept a binary file of arbitrary length, then split it into 4 pieces of approximately equal length without losing any of the data in the process.

  • 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-13T22:34:01+00:00Added an answer on June 13, 2026 at 10:34 pm

    Byte strings are not lists. You will have to write a separate function.

    But it is a simple translation. Assuming you have import qualified Data.ByteString as B, then

    chunkBytes :: Int -> B.ByteString -> [B.ByteString]
    chunkBytes n xs = chunk' i xs
      where
        chunk' n xs
            | B.null xs = []
            | otherwise = a : chunk' n b where (a,b) = B.splitAt n xs
        i = ceiling (fromIntegral (B.length xs) / fromIntegral n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this piece of legacy code which I'm trying to refactor at the
Well ive got this code that creates a table from JSON data. If a
I've got this code designed to echo a list of people from a specific
Ive got this code: loadData : function(jsonArray) { var id = $(this).attr(id); for(var i
I've got a UIView extension class called UIView+PDFView which takes the current view, splits
I've got this code: $filePath = '/blah/image.jpg'; $data_array = array( file_data=>@.$filePath, ); the $data_array
I've got this code on my page: header(Location: $page); $page is passed to the
I've got this code: Entry.h #import <Foundation/Foundation.h> @interface Entry : NSObject { id object;
I've got this code: def __parse(self): for line in self.lines: r = Record(line) self.records[len(self.records):]
I've got this code snippet, and I'm wondering why the results of the first

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.