Reading — step 1 of 5
Learn
~1 min readStrings and Show
Two foundational typeclasses for converting between values and their textual form:
show 42 -- "42"
show 3.14 -- "3.14"
show [1,2,3] -- "[1,2,3]"
show True -- "True"
(read "42" :: Int) -- 42 — explicit target type
(read "3.14" :: Double) -- 3.14
show converts ANY Show-able value to a String. Use it for debugging or when you need a generic textual representation.
read parses — but you usually need a type annotation, since read "42" could plausibly produce Int, Integer, Double, etc. read THROWS on parse failure; the safe variant is readMaybe :: String -> Maybe a from Text.Read.
For IO, print is putStrLn . show — it shows then prints:
print 42 -- 42
print [1,2,3] -- [1,2,3]
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…