问题
I know you can convert a String
to an number with read
:
Prelude> read "3" :: Int
3
Prelude> read "3" :: Double
3.0
But how do you grab the String
representation of an Int
value?
回答1:
The opposite of read
is show
.
Prelude> show 3
"3"
Prelude> read $ show 3 :: Int
3
回答2:
An example based on Chuck's answer:
myIntToStr :: Int -> String
myIntToStr x
| x < 3 = show x ++ " is less than three"
| otherwise = "normal"
Note that without the show
the third line will not compile.
回答3:
Anyone who is just starting with Haskell and trying to print an Int, use:
module Lib
( someFunc
) where
someFunc :: IO ()
x = 123
someFunc = putStrLn (show x)
来源:https://stackoverflow.com/questions/2784271/haskell-converting-int-to-string