Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
Case matching on read function in Haskell

I am wondering how to check success or failure (resulting in Prelude.read: no parse) of the read function in Haskell. In my case i run "(read formatted :: Int)" in code formatting a record structure, where the fields might be a single Int in String form but might also contain something else. I want to apply my function only to the fields where the read returns an Int. Thanks.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You should consider readMaybe from Text.Read. Once the value is returned within the Maybe monad then you can use case to decide what to do.

import Text.Read

add1 :: String -> Maybe Int
add1 str = case intval of  
   Just x -> Just (x + 1)
   Nothing -> Nothing

   where
      intval = readMaybe str


main = do
   print $ add1 "7"
   print $ add1 "7.0"

If you want to be more adventurous, now that the data is in the Maybe monad, we can treat the Maybe as a functor and use applicative functors to process them.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!