代码之家  ›  专栏  ›  技术社区  ›  Squirrelsama

haskell:将int转换为字符串

  •  169
  • Squirrelsama  · 技术社区  · 16 年前

    我知道你可以转换 String 到一个数字 read :

    Prelude> read "3" :: Int
    3
    Prelude> read "3" :: Double 
    3.0
    

    但你如何抓住 表示 Int 价值?

    3 回复  |  直到 7 年前
        1
  •  248
  •   Chuck    16 年前

    相反的 read show .

    Prelude> show 3
    "3"
    
    Prelude> read $ show 3 :: Int
    3
    
        2
  •  3
  •   prasad_    8 年前

    基于查克答案的一个例子:

    myIntToStr :: Int -> String
    myIntToStr x
        | x < 3     = show x ++ " is less than three"
        | otherwise = "normal"
    

    注意,没有 show 第三行不能编译。

        3
  •  0
  •   Arlind Hajredinaj    7 年前

    任何刚开始使用haskell并尝试打印int的人,请使用:

    module Lib
        ( someFunc
        ) where
    
    someFunc :: IO ()
    x = 123
    someFunc = putStrLn (show x)
    
    推荐文章