## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Finding Matching Digits

Our first problem is to find the digits that might be encoded at a given position. For the next while, we’ll make a couple simplifying assumptions. The first is that we’re working with a single row. The second is that we know exactly where in a row the left edge of a barcode begins.

## Run Length Encoding

How can we overcome the problem of not even knowing how thick our bars are? The answer is to run length encode (instead of repeating a value some number of times, run length encoding presents it once, with a count of the number of consecutive repeats):

```-- file: ch12/Barcode.hs
type Run = Int
type RunLength a = [(Run, a)]

runLength :: Eq a => [a] -> RunLength a
runLength = map rle . group
where rle xs = (length xs, head xs)```

The `group` function takes sequences of identical elements in a list and groups them into sublists:

````ghci> ``group [1,1,2,3,3,3,3]`
[[1,1],[2],[3,3,3,3]]
```

Our `runLength` function represents each group as a pair of its length and first element:

````ghci> ``let bits = [0,0,1,1,0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0]`
`ghci> ``runLength bits`