I am learning Haskell and want to use “readHex”, which according to Hoogle has type:
readHex :: Num a => ReadS a
How do you “extract” a result from such a function? What’s the most common way, pattern match against the right constructor ie, [(a,””)] ??
LiftM and lifting in general seems to make some sense, but I’m lost when it comes to “unwinding” the monadic stack.
To answer the general question in general terms, the only way to extract values from a data constructor is pattern matching. Some data types come with functions that extract values for you, but those functions are themselves implemented with pattern matching, or call other functions that are, &c. Abstract data types like
Data.Map.MaporIO, that want to hide their internal structure, still require pattern matching to work with; the difference is that they don’t export their constructors from the module that defines them, so all you have to work with are other functions defined in the module and the operations they provide.To answer the specific question,
ReadSis defined as such:So it’s just a type synonym. You don’t need to extract anything from the
ReadSitself, it’s just a shorthand or alias. The actual type is[(a, String)], which you can work with the same way you would anything else using lists, tuples,Strings, and so on.Furthermore,
ReadSis not aMonad. It’s a type synonym for something that isn’t aMonadinstance, and in fact can’t be made into one directly (there’s no way to write[(a, String)]in the form required for an instance declaration).