Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why “p” for first item in list in Haskell’s quicksort example?

Tags:

haskell

From haskell.org:

quicksort :: Ord a => [a] -> [a]
quicksort []     = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
    where
        lesser  = filter (< p) xs
        greater = filter (>= p) xs

My question is which word does “p” stand for here? In other words, why (p:xs) rather than (x:xs)?

like image 415
davidchambers Avatar asked Dec 27 '22 05:12

davidchambers


1 Answers

I just spotted the answer:

(p:xs) pattern on its left-hand side standing for non-empty list with the head element p (used as a pivot element)

“p” stands for “pivot”.

like image 101
davidchambers Avatar answered Feb 22 '23 23:02

davidchambers