Squ*_*ama 189 string int haskell casting
我知道您可以将a转换String为数字read:
Prelude> read "3" :: Int
3
Prelude> read "3" :: Double
3.0
Run Code Online (Sandbox Code Playgroud)
但是你如何抓住价值的String代表Int?
Chu*_*uck 270
相反的read是show.
Prelude> show 3
"3"
Prelude> read $ show 3 :: Int
3
Run Code Online (Sandbox Code Playgroud)
任何刚开始使用 Haskell 并尝试打印 Int 的人,请使用:
module Lib
( someFunc
) where
someFunc :: IO ()
x = 123
someFunc = putStrLn (show x)
Run Code Online (Sandbox Code Playgroud)
一个基于查克回答的例子:
myIntToStr :: Int -> String
myIntToStr x
| x < 3 = show x ++ " is less than three"
| otherwise = "normal"
Run Code Online (Sandbox Code Playgroud)
请注意,没有show第三行将无法编译。