# Solved: check if list is sorted

Sure, I will illustrate the process of checking whether a list in Haskell is sorted or not. This is a common operation in functional programming and it allows us to ensure that elements in a list follow a certain order, often from least to greatest (or vice versa).

Haskell, being a statically typed, purely functional programming language, provides us with various ways to solve this problem. It is well-known for its strong type inference, which will aid us significantly in this task.

##

## The Problem: Checking if a List is Sorted

As a Haskell developer, you are often required to ensure that the contents of a list follow a sorted order. This means the elements are arranged either in ascending or descending order. This is a common task that arises when dealing with user inputs, reading files, or managing data in general.

The simplest way to check if a list is sorted in Haskell is by comparing it with its sorted version. Haskell provides a built-in function, sort, which we can use to sort a list in ascending order. If the list remains the same after sorting, we can safely conclude that it was already sorted. Let’s see how we can do this:

```import Data.List
isSorted :: (Ord a) => [a] -> Bool
isSorted xs = xs == sort xs
```

But, this method is not optimal as it requires a full sort of the list which consumes time and resources especially for large lists.

##

## The Solution: Optimized Code

A list is sorted if for every pair of adjacent elements, the first one is less than or equal to the second one. To implement this, we will use the zipWith and all functions in combination. Here is the optimized code:

isSorted :: (Ord a) => [a] -> Bool
isSorted xs = all (uncurry (<=)) (zip xs (tail xs)) [/code] zip combines two lists into a list of pairs, while tail skips the first element and returns the rest. uncurry applies a binary operator to a pair, and all checks if a condition holds for all elements in the list. In our case, the condition is that for each pair, the first element should be lower or equal to the second.

##

## Step-by-Step Explanation of the Code

We can further understand the optimized code by dividing it into steps. The idea is to sequentially check each pair of elements in the list, and if we find any pair where the first element is greater than the second, we return False as the list is not sorted.

1. zip xs (tail xs) will take a list [1,2,3,4] and convert it into a list of pairs [(1,2),(2,3),(3,4)]. Each pair is basically the current element and the next element in the list.

2. We then use the all function which takes in a predicate (a function that returns a Bool) and a list, and returns True only if the predicate is true for all elements in the list.

3. Our predicate here is (uncurry (<=)). uncurry takes a function and a tuple, and applies the function to the elements of the tuple. <= is our function here, so uncurry (<=) would be a function that takes a tuple (a, b) and returns True if a <= b. This approach helps us solve the problem efficiently in linear time, providing a robust and efficient solution when dealing with potentially large lists. As for style in Haskell, the language promotes writing clean and concise code. So it's good style to split and abstract your code into small, composable functions and reasonable sections. Haskell highly appreciates that it lets you write beautiful, clear, and high-level code.

Related posts: