Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Converting Int to String

People also ask

How do I convert integer to string in Haskell?

Haskell is a statically typed, functional, and concurrent programming language with a dynamic range of packages. The show method is used to convert an int value to string .

How do you make a string in Haskell?

In Haskell it is also very easiest form of data type which can be used, easy to handle and create as well. String can be created by using double quotes (“”) inside this we can write our string or value that we want string type to be hold for us.

How do you concatenate strings in Haskell?

string s = String. Concat(a,b); C#

What is show in Haskell?

The shows functions return a function that prepends the output String to an existing String . This allows constant-time concatenation of results using function composition.


The opposite of read is show.

Prelude> show 3
"3"

Prelude> read $ show 3 :: Int
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)

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.


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