Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Pretty much what the title says. I have a list of Integers like so: [1,2,3]. I want to change this in to the Integer 123. My first thought was concat but that doesn't work because it's of the wrong type, I've tried various things but usually I just end up returning the same list. Any help greatly appreciated.

Also I have found a way to print the right thing (putStr) except I want the type to be Integer and putStr doesn't do that.

like image 355
Paul Avatar asked Dec 16 '09 23:12

Paul


People also ask

How do I turn a list into an integer?

Use int() function to Convert list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.

How does ++ work in Haskell?

The ++ operator is the list concatenation operator which takes two lists as operands and "combine" them into a single list. So if you have the list [x] and the list [y] then you can concatenate them like this: [x]++[y] to get [x, y ]. Notice that : takes an element and a list while ++ takes two lists.


1 Answers

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

joiner :: [Integer] -> Integer
joiner = read . concatMap show
like image 128
Ashutosh Mehra Avatar answered Oct 27 '22 11:10

Ashutosh Mehra