'Understanding the syntax in this merge function
I am a beginner at Haskell and have a question about the following merge function. In the following code we get two sorted lists as input and we should merge them together such that the output list is also sorted.
merge :: [int] -> [int] -> [int]
merge [] [] = []
merge x [] = x
merge [] x = x
--merge xs [] = xs
--merge [] ys = ys
merge (a:as) (b:bs)
| a<= b = a : (merge as (b:bs))
|otherwise = b : (merge (a:as) bs)
I have seen two versions of this code.
Let us say that x represents the first element of the list and when we want to merge one element with an empty list the result is going to be a list with one element. Because of what we defined in the function's head and representation of x or xs or ys, we don't need to write them in this way [x] or (x:)?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
