I am trying to use the reads function from Prelude, and doctest to test it. Just loading GHCI and typing reads "57x" or Prelude.reads "57x" yields only an empty list [], so I thought I had to import the function myself. According to the docs, it should return a tuple. But running Doctest and in GHCI, I get the error *** Exception: Prelude.read: no parse when putting in any test that has characters in addition to the Integer, ie 54x. What do I need to change to get it to return the proper tuple, as mentioned here, but with INTs instead of DOUBLEs?
I have a haskell file that looks like this:
module StackOverflow where
import Prelude hiding (words, reads)
reads :: String -> [(Int, String)]
-- ^ Takes a string, like "57" and reads the corresponding integer value
-- out of it. It returns an empty list if there is a failure, or a list
-- containing one tuple, with the integer value as the first element of
-- the tuple and a (possibly empty) string of remaining unconvertable extra
-- characters as the second element.
--
-- Examples:
--
-- >>> reads "57"
-- [(57,"")]
--
-- >>> reads "57x"
-- [(57,"x")]
--
reads s = [(read s :: Int,"")]
readsor define your own?module Test where import Preludewith or without theimport Prelude, typingreads "54x"orPrelude.reads "54x"with or without thex, I still just get an empty list[]. This is why I added the function myself, and had problems withwordsas noted in my SO question herereadspolymorphic signature, you need to provide a type signature so it knows what type you are expecting. Tryreads "54x" :: [(Int, String)]. I believe it defaults to the type[((), String)]if there is no type signature, and[]would be the correct result in that case.