Filter items from nested lists

I find it difficult to understand the logic of functional programming. So this should be something pretty simple, but not working for me. What I'm trying to do is: A function with a list of parameters and a list looks something like this: [[1,2,3], [5,7,8,12,34], [2,4,6,8]] and first I have to delete the values ​​less than 6, and then delete all the lists with less than two elements. My code is as follows:

f11 :: [[Int]] -> [[Int]]
f11 xs = ( filter (\s -> if a <= 6 a then True else False )) (map (filter (\x -> length x <= 2)) xs)
+4
source share
4 answers

You can also use notation do, thanks to instance lists Monad.

f11 xss = do
    xs <- xss
    let ys = [x | x <- xs, x >= 6]
    guard (length ys <= 2)
    return ys

do foreach. xs xss, , 6, ys. guard ys , 2.

+1

, :

f11 :: [[Int]] -> [[Int]]
f11  =  (filter \x-> length x < 2) . (map (filter \x -> x > 6))
+1

, :

f11 :: [[Int]] -> [[Int]]
f11 xs = 
    let 
        less6Cleared = map (filter (\x -> x >= 6)) xs
    in  
        filter (\x -> length(x) >= 2) less6Cleared                       

, less6Cleared - , 6. less6Cleared.

+1

...

f11 :: [[Int]] -> [[Int]]
f11 = (filter $ (>= 2) . length) . (map $ filter (>= 6))

... , , .

"", , 6 , 2 .

0

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


All Articles