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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:28:29+00:00 2026-05-24T18:28:29+00:00

import qualified Data.ByteString.Lazy.Char8 as BS stuff <- BS.readFile stuff.txt How do take a specific

  • 0
import qualified Data.ByteString.Lazy.Char8 as BS

stuff <- BS.readFile "stuff.txt"

How do take a specific character from a bytestring then change its ASCII and then put it back?
Do I use readInt or something?

Ex: “aaaaa” ,”a” is 97 so minus 1 and you have “aa`aa”

  • 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-24T18:28:30+00:00Added an answer on May 24, 2026 at 6:28 pm

    Others have addressed the problem of doing the byte operations, so I will focus on the other half of your question: selecting and updating a particular byte within a ByteString. Let’s start with implementing the operation for plain lists, using a more familiar interface:

    onNth :: Int -> (a -> a) -> ([a] -> [a])
    onNth n f xs = case splitAt n xs of
        (beginning, x:ending) -> beginning ++ f x : ending
        _ -> xs -- happens when n is out-of-bounds
    

    You might equivalently implement this using take and drop instead of splitAt. Now, how can we translate this to work on ByteStrings? Well, the ByteString interface offers take, drop, splitAt, append, and cons; the only thing we haven’t quite got available is the pattern matching that we did in the x:ending part above. Luckily, ByteString does offer something similar:

    uncons :: ByteString -> Maybe (Word8, ByteString)
    

    So, using that, we can write a new onNth function that works for ByteStrings:

    second :: (b -> c) -> (a, b) -> (a, c)
    second f (a, b) = (a, f b)
    
    onNth :: Int -> (Word8 -> Word8) -> (ByteString -> ByteString)
    onNth n f bs = case second uncons (splitAt n bs) of
        (beginning, Just (x, ending)) -> append beginning (cons (f x) ending)
        _ -> bs -- again, for out-of-bounds cases
    

    Finally, we can discuss what function we should use as the f :: Word8 -> Word8 argument above. Although you talk about text above, I will point out that you shouldn’t be using ByteString for text anyway (ByteStrings are sequences of bytes, not sequences of Chars). Therefore, if you have chosen to use ByteString, you must be talking about bytes, not text. 😉

    Therefore, you really meant to ask about a function which decreases a byte by one, presumably wrapping around on a boundary. subtract 1 is a function that does exactly that, so to convert pack [97, 97, 97, 97, 97] to pack [97, 97, 96, 97, 97], you might write onNth 2 (subtract 1). Reads almost like English!

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

Sidebar

Related Questions

Suppose I have: import qualified Data.ByteString.Lazy as L bs1 :: L.ByteString bs2 :: L.ByteString
I'm trying to parse a binary format (PES) using Haskell: import qualified Data.ByteString.Lazy as
I have the following snippet: import qualified Data.Vector as V import qualified Data.ByteString.Lazy as
I've got the following code: import Data.Binary.Get import qualified Data.ByteString.Lazy as L import qualified
I still don't get how bytestrings work import qualified Data.ByteString.Lazy as BS let x
i've wrote the following Haskell code import Data.Attoparsec (Parser) import qualified Data.Attoparsec.Char8 as A
I'm using the file-embed package thusly: import qualified Data.ByteString as B import qualified Data.ByteString.Internal
I write the following file temp.hs: import qualified Data.Set import System.Environment main :: IO
This is toy-example.hs: {-# LANGUAGE ImpredicativeTypes #-} import Control.Arrow data From = From (forall
I want to take n biggest elements from lazy list. I heard that mergesort

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.