Convert list of Integers into one Int (like concat) in haskell

This worked pretty well for me.

read (concat (map show (x:xs))) :: Int

How function reads:
Step 1 - convert each int in the list to a string (map show (x:xs))
Step 2 - combine each of those strings together (concat (step 1))
Step 3 - read the string as the type of int read (step 2) :: Int


Use read and also intToDigit:

joinInt :: [Int] -> Int
joinInt l = read $ map intToDigit l

Has the advantage (or disadvantage) of puking on multi-digit numbers.


You can use foldl to combine all the elements of a list:

fromDigits = foldl addDigit 0
   where addDigit num d = 10*num + d

The addDigit function is called by foldl to add the digits, one after another, starting from the leftmost one.

*Main> fromDigits [1,2,3]
123

Edit:
foldl walks through the list from left to right, adding the elements to accumulate some value.

The second argument of foldl, 0 in this case, is the starting value of the process. In the first step, that starting value is combined with 1, the first element of the list, by calling addDigit 0 1. This results in 10*0+1 = 1. In the next step this 1 is combined with the second element of the list, by addDigit 1 2, giving 10*1+2 = 12. Then this is combined with the third element of the list, by addDigit 12 3, resulting in 10*12+3 = 123.

So pointlessly multiplying by zero is just the first step, in the following steps the multiplication is actually needed to add the new digits "to the end" of the number getting accumulated.


You could concat the string representations of the numbers, and then read them back, like so:

joiner :: [Integer] -> Integer
joiner = read . concatMap show