How to input several values in one line in haskell - haskell

For example, I want to write a program which will take 3 integers as input from command line. The functions I have learned is readLn to read values from entire line. But readLn seems to parse the entire line as a single value. How can I get the three values of one line with haskell?

Read a line with getLine, split it into words, and read each:
readInts :: IO [Int]
readInts = fmap (map read.words) getLine
it reads any number of Ints:
ghci> readInts
1 2 3
[1,2,3]
ghci> readInts
1 2 3 4 5 6
[1,2,3,4,5,6]
You could restrict to three:
read3Ints :: IO [Int]
read3Ints = do
ints <- readInts
if length ints == 3 then return ints else do
putStrLn ("sorry, could you give me exactly three integers, "
++ "separated by spaces?")
read3Ints
which looks like this:
ghci> read3Ints
1 2
sorry, could you give me exactly three integers, separated by spaces?
1 23 , 5, 6
sorry, could you give me exactly three integers, separated by spaces?
1,2,3
sorry, could you give me exactly three integers, separated by spaces?
1 3 6
The secrets of fmap
fmap works a bit like map, but you can use it more widely:
ghci> fmap (*10) [1,2,3,4]
[10,20,30,40]
ghci> fmap (*10) (Just 5)
Just 50
ghci> map (fmap (*10)) [Left 'b', Right 4, Left 'c', Right 7]
[Left 'b',Right 40,Left 'c',Right 70]
ghci> fmap words getLine
Hello there me hearties!
["Hello","there","me","hearties!"]
In getInts, I did fmap (map read.words) to split the line by spaces, then map read to turn each one into an Int. The compiler knows I wanted Int because of the type signature - I'd get an error if I omitted it.

There several ways you could go about this, but I'd prefer the one using reads. This function has type Read a => ReadS a, where ReadS a is just a type alias for String -> [(a, String)]. It pulls characters from the provided string, stopping when it no longer successfully parses, then returns a list containing a tuple of the parsed value and the remaining string. It uses a list for a more flexible definition, but in practice you'll never see it return a list more than 1 element. An example would be
> reads "1 2 3" :: [(Int, String)]
[(1, " 2 3")]
> reads " 2 3" :: [(Int, String)]
[(2, " 3")]
Using this function, we can parse a line for Ints and it'll automatically stop when it fails to parse one or the string is empty. We just need a way to chain together multiple calls. Ideally, we'd like a function with the type (s -> [(a, s)]) -> s -> [a] so that we can re-use it:
chain :: (s -> [(a, s)]) -> s -> [a]
chain f s = case f s of
[] -> []
[(a, newS)] -> a : chain f newS
xs -> map fst xs ++ chain f (last $ map snd xs)
Then you can use it as
> chain reads "1 2 3" :: [Int]
[1, 2, 3]
> chain reads "1 2 3 asdf" :: [Int]
[1, 2, 3]
> chain reads "asdf 1 2 3" :: [Int]
[]
Then you just have to do
read3Ints :: String -> [Int]
read3Ints = take 3 . chain reads

What about:
getInts::[Int]
getInts = do s<-getLine
map read . words $ s

Related

Matching list of lists with other list of lists

Hey guys I have a question about matching lists
Given the list of lists:
Input List 1 = [[True],[False],[True],[[False]]
Input List 2 = [[Bob],[Rick],[Lee],[Bill]]
Input List 3 = [[1],[2],[3],[4]]
Then match the Booleans of list 1 with the other lists so this happens:
Input List 1 + Input List 2 = [[Bob],[Dead],[Lee],[Dead]]
Input List 1 + Input List 2 = [[1],[0],[3],[0]]
As far as I can see, using nested lists here is unnecessary. Without them, all you need is zipWith and an appropriate combining function:
-- The first argument is the "dead" value. Note that this works with any type.
makeDead :: a -> Bool -> a -> a
makeDead dead b val
| b = val
| otherwise = dead
GHCi> :t zipWith
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
GHCi> zipWith (makeDead "Dead") [True,False,True,False] ["Bob","Rick","Lee","Bill"]
["Bob","Dead","Lee","Dead"]
Two variations on the theme. Firstly, if whether a value should be changed depends only on what the value is, then you can simply use map/fmap:
-- Affects all names with 'i' as the second letter:
lethalLetter :: String -> String
lethalLetter name -> case name of
(_:'i':_) -> "Dead"
_ -> name
GHCi> fmap lethalLetter ["Bob", "Rick", "Lee", "Bill"]
["Bob","Dead","Lee","Dead"]
Secondly, you might prefer using Maybe to indicate deadness rather than using an arbitrary value (what if someone is actually called "Dead")?
makeDead' :: Bool -> a -> Maybe a
makeDead' b val
| b = Just val
| otherwise = Nothing
GHCi> zipWith makeDead' [True,False,True,False] ["Bob","Rick","Lee","Bill"]
[Just "Bob",Nothing,Just "Lee",Nothing]
You can then use functions like maybe, fromMaybe and catMaybes (the latter two are in Data.Maybe) to get rid of the Nothings however you feel like doing it:
GHCi> import Data.Maybe
GHCi> foo = zipWith makeDead' [True,False,True,False] ["Bob","Rick","Lee","Bill"]
GHCi> catMaybes foo
["Bob","Lee"]
Assuming you change them to lists, as recommended by duplode, how about zipping together the two lists within a list comprehension, something like this:
[if flag then val else defaultVal | (flag, val) <- zip(list1, list2)]
where defaultVal would have to be specified, but seems to be Dead and 0 for lists 2 and 3.
(I don't have access right now to Haskell, so the syntax may not be 100% there, but that's the idea.)
You can use zipWith . zipWith :: (a -> b -> c) -> [[a]] -> [[b]] -> [[c]] for matching lists of lists. For example:
import Data.Bool (bool)
kill deadVal = zipWith . zipWith $ bool (const deadVal) id
example = kill 0 [[True, False],[False,True]] [[1,2,3],[2]]
-- example = [[1,0],[0]]

How to get all subnumbers of a number in haskell

I would like to get all sub numbers of a number from a particular side.
In the case of the number 1234, the sub numbers from the left side are:
1, 12, 123, 1234
I implemented it with:
tail . inits $ show 1234
This way I get all the sub numbers in [[Char]] format.
["1","12","123","1234"]
I tried to convert them to Integer, with the following line.
map read . tail . inits $ show 1234
But I get the following error
[*** Exception: Prelude.read: no parse
What am I doing wrong?
because the interpreter does not know what type you want back
this will work:
λ> map read . tail . inits $ show 1234 :: [Int]
[1,12,123,1234]
of course you can just add a type-signature as well (most likely in your code file):
subnums :: Int -> [Int]
subnums = map read . tail . inits . show
in ghci:
λ> subnums 1234
[1,12,123,1234]
and a nice exercise can be to do this without show/read:
subnumbers :: Int -> [Int]
subnumbers 0 = []
subnumbers n =
n : subnumbers (n `div` 10)
Can you solve the problem with the order here?
A good approach is to use an unfold. While a fold (variously known as reduce, accumulate or aggregate in other languages) can process a list of numbers (or values of other types) to compute a single result value, an unfold starts with a single value and expands it into a list of values according to a given function. Let us examine the type of an unfold function:
Data.List.unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
We see that unfoldr take a function (b -> Maybe (a, b), and a starting b. The result is a [a]. If the function evaluates to Just (a, b), the a will be appended to the result, and the unfold recurses with the new b. If the function evaluates to Nothing, the unfold is complete.
The function for your unfold is:
f :: Integral b => b -> Maybe (b, b)
f x = if x > 0
then Just (x, x `div` 10) -- append x to result; continue with x/10
else Nothing -- x = 0; we're done
Now we can solve your problem without any of this show and read hackery:
λ> let f = (\x -> if x > 0 then Just (x, x `div` 10) else Nothing)
λ> reverse $ unfoldr f 1234
[1,12,123,1234]
As Carsten suggests, you need to give some indication of what type you want. This is because read is polymorphic in its result type. Until the compiler knows what type you want, it doesn't know what parser to use! An explicit type annotation is usually the way to go, but you might sometimes consider the function
asTypeOf :: a -> a -> a
asTypeOf x _ = x
how to use this here
I see two obvious ways to use asTypeOf here:
λ> asTypeOf (map read . tail . inits $ show 1234) ([0] :: [Int])
[1,12,123,1234]
and
λ> map (asTypeOf read length) . tail . inits $ show 1234
[1,12,123,1234]
the first one seems hardly better at all and the second might be a bit tricky for beginners - but it works ;)
Why? Because length has type [a] -> Int and so the result type will be fixed to Int:
λ> :t (`asTypeOf` length)
(`asTypeOf` length) :: ([a] -> Int) -> [a] -> Int
which is just what we need for read
Please note that it's not important what length does - only it's type is important - any other function with an compatible signature would have worked as well (although I can come up only with length right now)
For example:
wantInt :: [a] -> Int
wantInt = undefined
λ> map (asTypeOf read wantInt) . tail . inits $ show 1234
[1,12,123,1234]
A working list comprehension solution:
subNums :: Int -> [Int]
subNums num = [read x | let str = show num, let size = length str, n <- [1 .. size], let x = take n str]
λ> subNums 1234
[1,12,123,1234]

Project Euler 8 - I don't understand it

I looked up for a solution in Haskell for the 8th Euler problem, but I don't quite understand it.
import Data.List
import Data.Char
euler_8 = do
str <- readFile "number.txt"
print . maximum . map product
. foldr (zipWith (:)) (repeat [])
. take 13 . tails . map (fromIntegral . digitToInt)
. concat . lines $ str
Here is the link for the solution and here you can find the task.
Could anyone explain me the solution one by one?
Reading the data
readFile reads the file "number.txt". If we put a small 16 digit number in a file called number.txt
7316
9698
8586
1254
Runing
euler_8 = do
str <- readFile "number.txt"
print $ str
Results in
"7316\n9698\n8586\n1254"
This string has extra newline characters in it. To remove them, the author splits the string into lines.
euler_8 = do
str <- readFile "number.txt"
print . lines $ str
The result no longer has any '\n' characters, but is a list of strings.
["7316","9698","8586","1254"]
To turn this into a single string, the strings are concatenated together.
euler_8 = do
str <- readFile "number.txt"
print . concat . lines $ str
The concatenated string is a list of characters instead of a list of numbers
"7316969885861254"
Each character is converted into an Int by digitToInt then converted into an Integer by fromInteger. On 32 bit hardware using a full-sized Integer is important since the product of 13 digits could be larger than 2^31-1. This conversion is mapped onto each item in the list.
euler_8 = do
str <- readFile "number.txt"
print . map (fromIntegral . digitToInt)
. concat . lines $ str
The resulting list is full of Integers.
[7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4]
Subsequences
The author's next goal is to find all of the 13 digit runs in this list of integers. tails returns all of the sublists of a list, starting at any position and running till the end of the list.
euler_8 = do
str <- readFile "number.txt"
print . tails
. map (fromIntegral . digitToInt)
. concat . lines $ str
This results in 17 lists for our 16 digit example. (I've added formatting)
[
[7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[6,9,6,9,8,8,5,8,6,1,2,5,4],
[9,6,9,8,8,5,8,6,1,2,5,4],
[6,9,8,8,5,8,6,1,2,5,4],
[9,8,8,5,8,6,1,2,5,4],
[8,8,5,8,6,1,2,5,4],
[8,5,8,6,1,2,5,4],
[5,8,6,1,2,5,4],
[8,6,1,2,5,4],
[6,1,2,5,4],
[1,2,5,4],
[2,5,4],
[5,4],
[4],
[]
]
The author is going to pull a trick where we rearrange these lists to read off 13 digit long sub lists. If we look at these lists left-aligned instead of right-aligned we can see the sub sequences running down each column.
[
[7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[6,9,6,9,8,8,5,8,6,1,2,5,4],
[9,6,9,8,8,5,8,6,1,2,5,4],
[6,9,8,8,5,8,6,1,2,5,4],
[9,8,8,5,8,6,1,2,5,4],
[8,8,5,8,6,1,2,5,4],
[8,5,8,6,1,2,5,4],
[5,8,6,1,2,5,4],
[8,6,1,2,5,4],
[6,1,2,5,4],
[1,2,5,4],
[2,5,4],
[5,4],
[4],
[]
]
We only want these columns to be 13 digits long, so we only want to take the first 13 rows.
[
[7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[1,6,9,6,9,8,8,5,8,6,1,2,5,4],
[6,9,6,9,8,8,5,8,6,1,2,5,4],
[9,6,9,8,8,5,8,6,1,2,5,4],
[6,9,8,8,5,8,6,1,2,5,4],
[9,8,8,5,8,6,1,2,5,4],
[8,8,5,8,6,1,2,5,4],
[8,5,8,6,1,2,5,4],
[5,8,6,1,2,5,4],
[8,6,1,2,5,4],
[6,1,2,5,4],
[1,2,5,4]
]
foldr (zipWith (:)) (repeat []) transposes a list of lists (explaining it belongs to perhaps another question). It discards the parts of the rows longer than the shortest row.
euler_8 = do
str <- readFile "number.txt"
print . foldr (zipWith (:)) (repeat [])
. take 13 . tails
. map (fromIntegral . digitToInt)
. concat . lines $ str
We are now reading the sub-sequences across the lists as usual
[
[7,3,1,6,9,6,9,8,8,5,8,6,1],
[3,1,6,9,6,9,8,8,5,8,6,1,2],
[1,6,9,6,9,8,8,5,8,6,1,2,5],
[6,9,6,9,8,8,5,8,6,1,2,5,4]
]
The problem
We find the product of each of the sub-sequences by mapping product on to them.
euler_8 = do
str <- readFile "number.txt"
print . map product
. foldr (zipWith (:)) (repeat [])
. take 13 . tails
. map (fromIntegral . digitToInt)
. concat . lines $ str
This reduces the lists to a single number each
[940584960,268738560,447897600,1791590400]
From which we must find the maximum.
euler_8 = do
str <- readFile "number.txt"
print . maximum . map product
. foldr (zipWith (:)) (repeat [])
. take 13 . tails
. map (fromIntegral . digitToInt)
. concat . lines $ str
The answer is
1791590400
If you're not familiar with the functions used, the first thing you should do is examine the types of each function. Since this is function composition, you apply from inside out (i.e. operations occur right to left, bottom to top when reading). We can walk through this line by line.
Starting from the last line, we'll first examine the types.
:t str
str :: String -- This is your input
:t lines
lines :: String -> [String] -- Turn a string into an array of strings splitting on new line
:t concat
concat :: [[a]] -> [a] -- Merge a list of lists into a single list (hint: type String = [Char])
Since type String = [Char] (so [String] is equivalent to [[Char]]), this line is converting the multi-line number into a single array of number characters. More precisely, it first creates an array of strings based on the full string. That is, one string per new line. It then merges all of these lines (now containing only number characters) into a single array of characters (or a single String).
The next line takes this new String as input. Again, let's observe the types:
:t digitToInt
digitToInt :: Char -> Int -- Convert a digit char to an int
:t fromIntegral
fromIntegral :: (Num b, Integral a) => a -> b -- Convert integral to num type
:t map
map :: (a -> b) -> [a] -> [b] -- Perform a function on each element of the array
:t tails
tails :: [a] -> [[a]] -- Returns all final segments of input (see: http://hackage.haskell.org/package/base-4.8.0.0/docs/Data-List.html#v:tails)
:t take
take :: Int -> [a] -> [a] -- Return the first n values of the list
If we apply these operations to our string current input, the first thing that happens is we map the composed function of (fromIntegral . digitToInt) over each character in our string. What this does is turn our string of digits into a list of number types. EDIT As pointed out below in the comments, the fromIntegral in this example is to prevent overflow on 32-bit integer types. Now that we have converted our string into actual numeric types, we start by running tails on this result. Since (by the problem statement) all values must be adjacent and we know that all of the integers are non-negative (by virtue of being places of a larger number), we take only the first 13 elements since we want to ensure our multiplication is groupings of 13 consecutive elements. How this works is difficult to understand without considering the next line.
So, let's do a quick experiment. After converting our string into numeric types, we now have a big list of lists. This is actually kind of hard to think about what we actually have here. For sake of understanding, the contents of the list are not very important. What is important is its size. So let's take a look at an artificial example:
(map length . take 13 . tails) [1..1000]
[1000,999,998,997,996,995,994,993,992,991,990,989,988]
You can see what we have here is a big list of 13 elements. Each element is a list of size 1000 (i.e. the full dataset) down to 988 in descending order. So this is what we currently have for input into the next line which is, arguably, the most difficult-- yet most important-- line to understand. Why understanding this is important should become clear as we walk through the next line.
:t foldr
foldr :: (a -> b -> b) -> b -> [a] -> b -- Combine values into a single value
:t zipWith
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] -- Generalization of zip
:t (:)
(:) :: a -> [a] -> [a] -- Cons operator. Add element to list
:t repeat
repeat :: a -> [a] -- Infinite list containing specified value
Remember how I mentioned we had a list of 13 elements before (of varying-sized lists)? This is important now. The line is going to iterate over that list and apply (zipWith (:)) to it. The (repeat []) is such that each time zipWith is called on a subsequence, it starts with an empty list as its base. This allows us to construct a list of lists containing our adjacent subsequences of length 13.
Finally, we get to the last line which is pretty easy. That said, we should still be mindful of our types
:t product
product :: Num a => [a] -> a -- Multiply all elements of a list together and return result
:t maximum
maximum :: Ord a => [a] -> a -- Return maximum element in the list
The first thing we do is map the product function over each subsequence. When this has completed we end up with a list of numeric types (hey, we finally don't have a list of lists anymore!). These values are the products of each subsequence. Finally, we apply the maximum function which returns only the largest element in the list.
EDIT: I found out later what the foldr expression was for. (See comments bellow my answer).
I think that this could be expressed in different way - You can simply add a guard at the end of the list.
My verbose version of that solution would be:
import Data.List
import Data.Char
euler_8 = do
let len = 13
let str1 = "123456789\n123456789"
-- Join lines
let str2 = concat (lines str1)
-- Transform the list of characters into a list of numbers
let lst1 = map (fromIntegral . digitToInt) str2
-- EDIT: Add a guard at the end of list
let lst2 = lst1 ++ [-1]
-- Get all tails of the list of digits
let lst3 = tails lst2
-- Get first 13 digits from each tail
let lst4 = map (take len) lst3
-- Get a list of products
let prod = map product lst4
-- Find max product
let m = maximum prod
print m

List of ints to string - haskell?

I am trying to take a list of integers and repeat them a number of times, but as a string in haskell. The expected output is:
> nnn [3,1,5] = ["3-3-3","1","5-5-5-5-5"]
> nnn [10,2,4] = ["10-10-10-10-10-10-10-10-10-10","2-2","4-4-4-4"]
Now I think I am close... But I can't quite turn a list of ints into a String, and definitely don't know what to do about the hyphen. Here's my code so far, and output:
nnn :: [Int] -> [[Char]]
nnn list = map show (map (\x -> take x $ repeat x) list)
And it gives me:
> nnn [3,1,5] = ["[3,3,3]","[1]","[5,5,5,5,5]"]
(I'm close at least!) Can anyone point me in the right direction to look here? :)
A separate function to repeat a number into a hyphen-separated string seems useful here. Convert the number to a string first, replicate it, and use intercalate to insert a hyphen between each element and join them:
repeatNumber :: Int -> String
repeatNumber n = intercalate "-" $ replicate n $ show n
Then nnn is nice and simple:
nnn :: [Int] -> [String]
nnn = map repeatNumber
(replicate a b being take a $ repeat b)

Haskell list of integers to [5,5,5] -> [555] without Data.Lists.Split

Data.Lists.Split is actually not working winHugs interpreter. which can be get by splitEvery 3 ['a'..'z'] function
is it possible to achieve this without Data.Lists.Split ?
list of separate integers [5,5,5,6,6,6] -> to be [555,666] to concat every three numbers !
should be [Int]->[Int]
Yes, it's quite possible. You can proceed like this:
First write a function that takes three digits and "concat"s them into one number (this is quite easy using multiplication and addition).
Then you write another function which takes a list and processes its elements in threes using the previous function. Basically the definition of that function will look like the definition of map, except that you process three elements instead of one in the non-empty case (i.e. you use a pattern like x1:x2:x3:xs), and you call the previously defined function instead of one given as an argument.
Here's an implementation of splitEvery that should work for your purposes:
import Data.List
splitEvery _ [] = []
splitEvery n xs = chunk : splitEvery n remaining
where (chunk, remaining) = splitAt n xs
Let's make a general function that groups a given number of items, and applies a given function to each group.
groupMap :: ([a] -> b) -> Int -> [a] -> [b]
groupMap f n xs = go xs
where go [] = []
go xs = f (take n xs) : go (drop n xs)
OK, now what f should we pass in?
intListToInt :: [Int] -> Int
intListToInt xs = go xs 0
where go [] v = v
go (x:xs) v = go xs (x + v*10)
There are various different ways you could write this; I'm experimenting with the workhorse helper technique. Note that this type signature lines up nicely with the first argument of groupMap. Anyways...let's see if it works!
ghci> groupMap intListToInt 3 [5,5,5,6,6,6]
[555,666]
Yay! It even works for other stuff too. (some sketchy uses follow...)
ghci> groupMap (read :: String -> Int) 5 "1234554321"
[12345,54321]
ghci> groupMap (read :: String -> Bool) 4 "TrueTrueTrue"
[True,True,True]
ghci> groupMap sum 2 [1,2,3,4,5,6]
[3,7,11]
NB: If I'm not mistaken, intListToInt could benefit from a little more strictness:
{-# LANGUAGE BangPatterns #-}
intListToInt xs = go xs 0
where go [] !v = v
go (x:xs) !v = go xs (x + v*10)
Though I'm not at all familiar with which language pragmas Hugs supports.
Use show, concat and read:
map (read . (>>= show)) . splitEvery 3
Possible Solution:
Here is one possible solution, using only function from the Prelude:
group3 :: [Int] -> [Int]
group3 [] = []
group3 xs = toSingleNum (take 3 xs): (group3 (drop 3 xs))
where toSingleNum ys = read $ concat $ map show ys
There are, of course, innumerable ways to do this. This is one.
Explanation:
group3 works by using the take and drop functions and natural recursion to split the list into groups of 3, and applying toSingleNum to each of the groups.
Ignoring the application of take and drop, the function works roughly as below:
group3 [1,2,3,4,5,6,7,8,9]
toSingleNum [1,2,3] : group3 [4,5,6,7,8,9]
toSingleNum [1,2,3] : toSingleNum [4,5,6] : group3 [7,8,9]
toSingleNum [1,2,3] : toSingleNum [4,5,6] : toSingleNum [7,8,9] : group3 []
toSingleNum [1,2,3] : toSingleNum [4,5,6] : toSingleNum [7,8,9] : []
After toSingleNum is applied, we'd have:
123 : 456 : 789 : []
[123, 456, 789]
toSingleNum converts a list of numbers into a single number.
toSingleNum [1, 3, 4]
>>> 134
toSingleNum [12, 13, 14]
>>> 121314
The conversion in toSingleNum is done by converting the numbers to a String, then combining them, then converting back to a number.
toSingleNum [1, 3, 4]
read $ concat $ map show [1, 3, 4]
read $ concat $ ["1", "3", "4"]
read $ "134"
134
Further Reading:
If you're interested in Haskell, I'd recommend Graham Hutton's excellent "Programming in Haskell", or one of the tutorials listed on the Haskell Wiki.
First, split the list into chunks:
chunk n = unfoldr split
where
split [] = Nothing -- stop
split xs = Just $ splitAt n xs -- keep going
ghci> chunk 3 [1..6]
[[1,2,3],[4,5,6]]
The chunk function continues chopping n-sized chunks off the input list, until the list is empty. (unfoldr is one of the most under-appreciated functions in the standard library, by the way.)
Then turn each chunk into a number:
cumulate = foldl' (\x y -> x * 10 + y) 0
ghci> cumulate [4,5,6]
456
Now, compose these two functions together:
coalesce = map cumulate . chunk
ghci> coalesce [5,5,5,6,6,6]
[555,666]

Resources