Make function shorter in Haskell

Is it possible to cancel the variable x in this function ?:

numocc x = map (length.filter(==x))

For example, if I have this function:

numocc x l = map (countt x) l

I can end the variable l and get the following:

numocc x = map (countt x)

Thanks for the help.

+4
source share
1 answer

You can use pointfree to answer questions such as

> pointfree "numocc x = map (length . filter (==x))"

numocc = map . (length .) . filter . (==)

change

here he is in action

> let numocc x = map (length . filter (==x))
> let numocc' = map . (length .) . filter . (==)

numocc 'a' ["aa", "bb"] --outputs [2, 0]
numocc' 'a' ["aa", "bb"] --also outputs [2, 0]

Basically, it counts the number of param1 in each element in the elements of the param2 list

numocc 'a' ["aa", "bba"] --outputs [2, 1]
numocc' 'a' ["aa", "bba"] --also outputs [2, 1]
+4
source

All Articles