Group Argument By Lambda

Get to know you in Haskell shows a function groupBy:

ghci> let values = [-4.3, -2.4, -1.2, 0.4, 2.3, 5.9, 10.5, 
                          29.1, 5.3, -2.4, -14.5, 2.9, 2.3]  
ghci> groupBy (\x y -> (x > 0) == (y > 0)) values  
[[-4.3,-2.4,-1.2],[0.4,2.3,5.9,10.5,29.1,5.3],[-2.4,-14.5],[2.9,2.3]] 

In the groupByfirst argument, what is the meaning of lambda-2 arguments: xand y?

+4
source share
5 answers

These are variables for comparison. You know that groupcombines equal neighboring values. To decide what is equivalent, a comparison function is used. grouprelies on an instance of your Eqtypeclass type . But groupByit allows you to choose how to compare neighboring values.

+2
source

If we look at the type groupBy:

groupBy :: (a -> a -> Bool) -> [a] -> [[a]]

groupBy - , a a Bool.

groupBy comparer values where comparer x y = (x > 0) == (y > 0)

\x y -> , x y, .

, , - :

ghci> groupBy (\x y -> (x > 0) == (y > 0)) values
[[-4.3,-2.4,-1.2],[0.4,2.3,5.9,10.5,29.1,5.3],[-2.4,-14.5],[2.9,2.3]]

, , , . groupBy , . :

ghci> groupBy (\x y -> x == y) [1, 1, 2, 2, 2, 3, 3, 4]
[[1,1],[2,2,2],[3,3],[4]]
ghci> groupBy (\x y -> x == y) [1, 1, 2, 2, 2, 3, 3, 1]
[[1,1],[2,2,2],[3,3],[1]]

, 1 , .

+1

! groupBy Data.List, base Hackage. , , Hoogle , . Haddock, "Source" , . groupBy.

, .

-- | The 'groupBy' function is the non-overloaded version of 'group'.
groupBy                 :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy _  []           =  []
groupBy eq (x:xs)       =  (x:ys) : groupBy eq zs
                           where (ys,zs) = span (eq x) xs

, groupBy - group, base. group, , - By, ( , -, Eq , ).

, , , span ( Hackage!). span (2-) , , ( break, () ).

, , groupBy , , "" . , ( ), , , , !

+1

group. , . .

group [0,1,2,3,3,4] = [[0],[1],[2],[3,3],[4]]

GHC group :

group :: Eq a => [a] -> [[a]]
group =  groupBy (==)

, . , groupBy as;

groupBy (\x y -> x == y) [0,1,2,3,3,4] = [[0],[1],[2],[3,3],[4]]

, GHC groupBy:

groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy _  []           =  []
groupBy eq (x:xs)       =  (x:ys) : groupBy eq zs
                           where (ys,zs) = span (eq x) xs

eq . groupBy , . , (x:ys) , , . , span , .

, , 0.4, , 0.4 zs .

0

groupBy "". groupBy (\x y -> x `someComparison` y) someList, x "" , y someList. groupBy someList, y someList. , False. y . , x, someList y.

groupBy (1 st 2 nd 2 nd 3 rd ..), first , . :

groupBy (\x y -> x < y) [1,2,3,2,1] -- returns: [[1,2,3,2],[1]]

groupBy:

  • 1 2. 1 < 2, . : [[1,2]]
  • 1 3. 1 < 3, , 3 . : [[1,2,3]]
  • 1 2. 1 < 2, , 2 . : [[1,2,3,2]]
  • 1 1. 1 ≮ 1, , 1 - . : [[1,2,3,2],[1]]

: , groupBy, , :

groupBy (\x y -> x < y) [1,2,3,4,5,4,3,2,1] -- [[1,2,3,4,5,4,3,2],[1]]
groupBy (\x y -> x < y) [1,3,5,2,1] -- [[1,3,5,2],[1]]
groupBy (\x y -> x <= y) [3,5,3,2,1,0,1,0] -- [[3,5,3],[2],[1],[0,1,0]]
groupBy (\x y -> x <= y) [1,2,3,2,1] -- [[1,2,3,2,1]]

, groupBy , , first . False, , .

, groupBy , :

groupBy                 :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy _  []           =  []
groupBy eq (x:xs)       =  (x:ys) : groupBy eq zs
                           where (ys,zs) = span (eq x) xs

The key here is to use the function span: span (1<) [2,3,2,1]returns ([2,3,2],[1]). span (eq x) xsin the above code puts all the elements xsthat match (eq x)in the first part of the pair, and the rest xsin the second. (x:ys)then joins xwith the first part of the pair, and is groupByrecursively called in the rest of the pair xs(which is equal zs). That is why it groupByworks in such a strange way.

0
source

Source: https://habr.com/ru/post/1540625/


All Articles