Explain this Merge Function in Haskell

I am studying Haskell and it is difficult for me to understand this function. I am implementing mergesort. I have implemented a recursive mergesort function, but I don’t understand what this "merge" function does. I understand merge sorting in an imperative language, but here I don't understand the syntax.

merge []         ys                   = ys
merge xs         []                   = xs
merge xs@(x:xt) ys@(y:yt) | x <= y    = x : merge xt ys
                          | otherwise = y : merge xs yt
+4
source share
3 answers
merge []         ys                   = ys

If the first argument is empty, specify the second argument.

merge xs         []                   = xs

If the second argument is empty, specify the first argument.

merge xs@(x:xt) ys@(y:yt) | x <= y    = x : merge xt ys
                          | otherwise = y : merge xs yt

x y, ( ) x xs ( xt) ys. y , xs ys ( yt).

xs@(x:xt) - "placeholder". xs , x - , xt - .

, cons xs ys, , .

(|) "" , .

+11

:

  • merge [] ys = ys

    . (.. []), .

  • merge xs [] = xs

    , , .

  • merge xs@(x:xt) ys@(y:yt)

    , (x:xt), . x, , xt - . , : - (.. 1 : [2, 3] == [1, 2, 3]). xs@... , xs. , , .

+2

. :

merge []         ys                   = ys

, ys ys.

merge xs         []                   = xs

, : xs xs.

merge xs@(x:xt) ys@(y:yt) | x <= y    = x : merge xt ys
                          | otherwise = y : merge xs yt

. , :

  • xs ( as-pattern) x xt
  • ys - , y yt.

, ( ), y, ( xt) ys. , y , x.

+2

All Articles