Concatenation in Haskell

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Haskell

In Haskell, there are different ways to concatenate strings.

One way is to use the ++ operator. The ++ operator is used to append two strings together to form a new string. Here's an example:

concatenateString :: String -> String -> String
concatenateString s1 s2 = s1 ++ s2

main = do
let str1 = "Hello "
str2 = "World!"
putStrLn (concatenateString str1 str2)
-- Output will be "Hello World!"

In this example, we define a function named concatenateString that takes two String arguments, s1 and s2. We then use the ++ operator to concatenate s1 and s2 together to form a new string.

Another way to concatenate strings is by using the concat function. The concat function takes a list of strings and concatenates them together into a single string. Here's an example:

concatenateList :: [String] -> String
concatenateList sList = concat sList

main = do
let sList = ["Hello ", "World!"]
putStrLn (concatenateList sList)
-- Output will be "Hello World!"

In this example, we define a function named concatenateList that takes a list of String arguments, sList. We then use the concat function to concatenate all the elements in sList together to form a new string.