Haskell: Converting Int to String

ID : 16205

viewed : 179

Tags : stringhaskellintcastingstring





Top 5 Answer for Haskell: Converting Int to String

vote vote

96

The opposite of read is show.

Prelude> show 3 "3"  Prelude> read $ show 3 :: Int 3 
vote vote

87

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) 
vote vote

79

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.

vote vote

69

You can use show:

show 3 

What I want to add is that the type signature of show is the following:

show :: a -> String 

And can turn lots of values into string not only type Int.

For example:

show [1,2,3]  

Here is a reference:

https://hackage.haskell.org/package/base-4.14.1.0/docs/GHC-Show.html#v:show

vote vote

54

Top 3 video Explaining Haskell: Converting Int to String







Related QUESTION?