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

The Archive Base Latest Questions

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

Ive got two two functions which act on byte strings. The first one is

  • 0

Ive got two two functions which act on byte strings. The first one is a bytestring version of the cycle function in Data.List and the second function rotates the bytestring.

When I read a file and send its input to the rotateBytes function, it outputs a quotation mark and then I need to press Control-C to manually stop the function. Is this a bug in my code or a bug in ghc? How can it be fixed?

import qualified Data.ByteString as B

-- Cycle function for binary data
cycleBytes :: B.ByteString -> B.ByteString 
cycleBytes xs 
    | B.null xs = error "cycleBytes: empty list"
    | otherwise = xs' where xs' = xs `B.append` xs'

-- Rotate function for binary data
rotateBytes :: B.ByteString -> Int -> B.ByteString
rotateBytes xs n = B.take (B.length xs) $! B.drop (B.length xs + n) $! cycleBytes xs

Using the function is currently like this:

*Main> B.readFile "test.dat" >>= (\x -> return $ rotateBytes x 3)
"
^CInterrupted.
  • 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:40:20+00:00Added an answer on June 13, 2026 at 10:40 pm

    ByteString isn’t lazy so can’t cope with the infinite answer from cycleBytes. (The " you’re getting is because when printing the result, it can lazily get the first character of output without worrying about the rest, but then tries to calculate the infinite ByteString it gets from cycleBytes before printing anything else. Infinity and strict evaluation don’t mix.)

    1. Import Data.ByteString.Lazy instead (and use fromIntegral on your n, because it wants Int64 not Int; You might want to use Lazy ByteStrings for enormous amounts of data, so you the library needs a length parameter that allows this.)
    2. or (better) reduce n mod B.length xs and just use B.append xs xs instead of infinitely many.

    Solution 1 looks like

    import qualified Data.ByteString.Lazy as B
    
    -- Cycle function for binary data
    cycleBytes :: B.ByteString -> B.ByteString 
    cycleBytes xs 
        | B.null xs = error "cycleBytes: empty list"
        | otherwise = xs' where xs' = xs `B.append` xs'
    
    
    -- Rotate function for binary data
    rotateBytes :: B.ByteString -> Int -> B.ByteString
    rotateBytes xs n = B.take (B.length xs) 
                     $ B.drop (B.length xs + fromIntegral n) 
                     $ cycleBytes xs
    

    If test.dat contained Hello Mum! this would calculate Chunk "lo Mum!" (Chunk "Hel" Empty), because it’s lazily constructed and will only get combined if it’s forced.

    Solution 2 looks like

    rotateBytes :: B.ByteString -> Int -> B.ByteString
    rotateBytes xs n = 
        let len = B.length xs
            n' = n `mod` len in
         B.take len $! B.drop n' $! B.append xs xs
    

    Solution 2 is better partly because you get to keep everything strict (which I assume you were trying to do), but doesn’t have cycleBytes.

    Ben Millwood suggests replacing this with something along the lines of

    rotateBytes :: B.ByteString -> Int -> B.ByteString
    rotateBytes xs n = 
        let n' = n `mod` B.length xs 
            (beginning,end) = B.splitAt n' xs in
        B.append end beginning
    

    which reads more clearly. take, drop and splitAt are all O(1) in ByteString, so it doesn’t make much difference for efficiency, but the splitAt does feel cleaner.

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

Sidebar

Related Questions

I've got two functions and one problem. hideTable(); ajaxCall(params); The function hideTable function hideTable()
I've got a function that takes two strings as arguments, and I want to
I have two C functions, which basically operate on a stack data structure. This
I've got two recursive functions: 1) function getCategories($id) { global $con; $select = $con->prepare('SELECT
I've got two aspx pages which are very similar and have various identical functions
I've got different functions in actionscript 3, one function generates random numbers each time
I'm trying to refactor my little voting functions. I've got two of them which
I've got a very simple VBA function that takes two range arguments and returns
I've got two long lists A and B which have the same length but
I've got two controllers: admin and customers, plus one more called sessions for handling

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.