Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate Integers as Strings in Haskell?

I want to concatenate strings in Haskell and also integers from function like this:

arc 13 34 234 3

13 34 234 3 will be arguments of arc function and I want output like

"arc(13, 34, 234, 3)"

as a String how can I implement this?

like image 250
Mahmut Bulut Avatar asked Dec 07 '22 16:12

Mahmut Bulut


1 Answers

How can list of numbers could be concatenated into a string? Looks like some [Int] -> String function can help here.

> concat . intersperse ", " . map show $ [13, 34, 234, 3]
"13, 34, 234, 3"

So, let's add some brackets and "arc" to that String.

import Data.List (intersperse)

arc :: [Int] -> String
arc nums = "arc(" ++ (concat . intersperse ", " . map show $ nums) ++ ")"

And we get the answer.

> arc  [13, 34, 234, 3]
"arc(13, 34, 234, 3)"

If you are really need function with signature like Int -> Int -> Int -> Int -> String:

arc' :: Int -> Int -> Int -> Int -> String
arc' a1 a2 a3 a4 = "arc(" ++ (concat . intersperse ", " . map show $ [a1,a2,a3,a4]) ++ ")"

> arc' 13 34 234 3
"arc(13, 34, 234, 3)"
like image 176
ДМИТРИЙ МАЛИКОВ Avatar answered Dec 09 '22 14:12

ДМИТРИЙ МАЛИКОВ