Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

313
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda