Closing and Listing Concepts in Haskell

I am playing with Haskell right now and thus came across a list comprehension function. Naturally, I would use closure to do things like this:

Prelude> [x|x<-[1..7],x>4] -- list comprehension [5,6,7] Prelude> filter (\x->x>4) [1..7] -- closure [5,6,7] 

I still don’t feel this language, and how will the Haskell programmer be? What are the differences between the two solutions?

+6
source share
2 answers

Idiomatic Haskell will filter (> 4) [1..7]

Note that you do not commit the lexical region in your closure and use the partitioned operator instead. In other words, you need a partial application > that operators provide you with immediately. The list of concepts is sometimes attractive, but the usual perception is that they do not scale as well as the usual set of higher-order functions (β€œscale” for more complex compositions). Such a stylistic decision, of course, is largely subjective, so YMMV.

+10
source

List attributes come in handy if the elements are somewhat complex and need to be filtered out by matching with the pattern, or the display part seems too complicated for the lambda abstraction, which should be short (or, it seems to me), or if one deals with nested lists. In the latter case, understanding the list is often more readable than alternatives (for me, anyway).

For example, something like:

 [ (fb, (g . fst) a) | (Just a, Right bs) <- somelist, a `notElem` bs, (_, b) <- bs ] 

But for your example, section (>4) is a very good way to write (\a -> a > 4) , and since you only use it for filtering, most people would prefer ANthonys solution.

+1
source

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


All Articles