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

The Archive Base Latest Questions

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

I have a list with a string inside it and I need to have

  • 0

I have a list with a string inside it and I need to have that deconstructed Char by Char and put into a list as Integer instead but I’m stymied by the types

What i have is a txt file that i read into monad:

getTxt = do
  y <- readFile "foo.txt"
  return y

foo only contains this:

"1234567890\n"

then I thought I was close with sequence but that gets me this list:

["1","2","3","4","5","6","7","8","9","0"] :: [[Char]]

but I need [Integer]. ord will take Char -> Int but how do I read that [Char] -> [Int] ? And after all these trial and only error, don’t I need to filter out that last new line in the end?

Any suggestions?

  • 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-12T06:06:04+00:00Added an answer on June 12, 2026 at 6:06 am

    If you use ord, the types match, but it’s not what you want because ord gives you
    the ascii value, not the numeric value: ord 5 is 53, not 5. You could subtract
    48 to get the digit, then roll the digits up into a single number, but it would be
    easier to use a library function. The most straightforward choice is read:

    getInt :: IO Integer
    getInt = do
        y <- readFile "foo.txt"
        return (read (takeWhile (/='\n') y))
    

    As in the linked answer,
    the best solution here is to use reads.

    reads finds a list of possible matches,
    as pairs of (match,remainingstring),
    which works well for you because it will automatically leave the newline in the remaining string,

    *Main> reads "31324542\n" :: [(Integer,String)]
    [(31324542,"\n")]

    Let’s use that:

    findInt :: String -> Maybe Integer
    findInt xs = case reads xs of              -- have a look at reads xs
        ((anint,rest):anyothers) -> Just anint -- if there's an int at the front of the list, just return it
        _ -> Nothing                           -- otherwise return nothing
    

    Maybe‘s a handy data type that lets you have failure without crashing the program or doing exception handling.
    Just 5 means you got output and it’s 5. Nothing means there was a problem, no output.

    addTen :: FilePath -> IO ()
    addTen filename = do
        y <- readFile filename
        case findInt y of 
           Just i -> putStrLn ("Added 10, got "++show (i+10))
           Nothing -> putStrLn ("Didn't find any integer at the beginning of " ++ filename)
    

    Which gives you:

    *Main> addTen "foo.txt"
    Added 10, got 1234567890


    If you just want the integers the characters represent, you can put import Data.Char at the top of your file and do

    ordzero = ord '0'   -- handy constant, 48, to unshift the ascii code to a digit.
    
    getInts :: FilePath -> IO [Int]          -- ord gives the smaller Int, not Integer
    getInts filename = do
        y <- readFile filename
        return [ord achar - ordzero | achar <- takeWhile isDigit y]
    

    This takes the characters of the string y for as long as they’re digits,
    then finds their ord, subtracting ord '0' (which is 48) to turn '4' into 4 etc.

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

Sidebar

Related Questions

I have a textblock inside a list view that I need to hide or
I have list of files stored inside arraylist that I need to download in
I have List[(String,String)] and I need to sort them by the 2nd value and
I have a List of String objects that are pipe delimited. something like this:
I have a list of string values that I want add to a hashtable
I have a list of string that I am writing out to a CSV
I have a string list (TStrings) that has a couple thousand items in it.
I have an ASP.NET web app that places several string data into an object
In my app i have a list view, in that i need to place
I have a list of string like: s = [(abc,bcd,cde),(123,3r4,32f)] Now I want to

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.