Haskell map basics

I myself am learning Haskell and ran into a function problem mapand was looking for help. What I'm trying to do is take a list of lists and, if the list is empty, that is [], and one of the other lists has 1in my head. Then grab it 1and put it on the list [].

a = [[2,1,3,1],[],[2,3],[1,2,3],[4]]

and do it

a = [[2,1,3,1],[1],[2,3],[2,3],[4]]

So far, I have something like that I do not have the full thing.

((map (\k-> if ([])&&(elem 1 heads))a) 
where heads = map head a

But I also have to have it so that if it a = [[1,2,1,3,1],[],[2,3],[1,2,3],[4]] gives a = [[2,1,3,1],[1],[2,3],[1,2,3],[4]], so it only moves one 1to the empty list.

I know this will not work, but I鈥檝e been trying to do this for a couple of hours, but I鈥檓 not getting anything

+4
source share
4 answers

, . :

fun :: Eq a => a -> [[a]] -> [[a]]
fun z xss = newList 
  where (foundZ, newList, _) = foldl ... (False, [], False) xss

, , , , 1 ( z). , ( ). ( ), foundZ , foldl, , , -, . foldl:

\(fn,xss',rpl) xs ->
  case xs of 
    ...

xs: , , z :

    [] | not rpl -> ...
    (x:r) | x == z && not fn -> ...
    _  -> (fn, xs:xss',rpl)

, x == z, not fn, z , , , . not rpl . , .

    [] | not rpl -> (fn, (if foundZ then [z] else []):xss', True)

, z - , , z.

    (x:r) | x == z && not fn -> (True , r:xss', rpl) 

, z, True found ( , 1 ).

, :

fun :: Eq a => a -> [[a]] -> [[a]]
fun z xss = reverse newList 
  where (foundZ, newList, _) = 
          foldl 
            (\(fn,xss',rpl) xs ->
              case xs of 
                [] | not rpl -> (fn, (if foundZ then [z] else []):xss', True)
                (x:r) | x == z && not fn -> (True , r:xss', rpl) 
                _  -> (fn, xs:xss', rpl)
            ) (False, [], False) xss 

>fun 1 [[2,1,3,1],[],[2,3],[1,2,3],[4]]
[[2,1,3,1],[1],[2,3],[2,3],[4]]
>fun 1 [[1,2,1,3,1],[],[2,3],[1,2,3],[4]]
[[2,1,3,1],[1],[2,3],[1,2,3],[4]]
+1

. , .

doStuff :: Integral a => [[a]] -> [[a]]
doStuff xs = let n = min holes donors in go n n xs
  where go _ _ []          = []
        go 0 m ([]:ys)     = []     : go 0       m ys
        go n m ([]:ys)     = [1]    : go (n - 1) m ys
        go n 0 ((1:zs):ys) = (1:zs) : go n       0 ys
        go n m ((1:zs):ys) = zs     : go n (m - 1) ys
        go n m (y:ys)      = y      : go n m       ys
        holes              = count null    xs
        donors             = count goodish xs
        count f            = length . filter f
        goodish (1:_)      = True
        goodish _          = False

:

位> doStuff [[2,1,3,1],[],[2,3],[1,2,3],[4]]
[[2,1,3,1],[1],[2,3],[2,3],[4]]
位> doStuff [[1,2,1,3,1],[],[2,3],[1,2,3],[4]]
[[2,1,3,1],[1],[2,3],[1,2,3],[4]]
位> doStuff [[1,2,1,3,1],[],[2,3],[1,2,3],[4],[],[]]
[[2,1,3,1],[1],[2,3],[2,3],[4],[1],[]]
+3

, , . . , 1. , . foldl' , , , , ; .

+1

IMO map . map , , . , map, "" , lambdas, scoping .., map.

" 1 []". ( ) , haskell.

-: "" 1, , 1 , []. ( x) , 1 (y), , ,

  • min(x,y) [1]
  • tail min(x,y) , 1

, :

redistributeOnes :: [[Int]] -> [[Int]]
redistributeOnes xs = redistributeOnes' n n xs where
  n = min (length $ filter (==[]) xs) (length $ filter (\l -> if l == [] then False else (head l) == 1) xs)
  redistributeOnes' _ _ [] = []
  redistributeOnes' 0 0 xs = xs
  redistributeOnes' n m ([]:xs) | n > 0 = [1] : redistributeOnes' (n-1) m xs
                                | otherwise = [] : redistributeOnes' n m xs
  redistributeOnes' n m (x:xs)  | head x == 1 && m > 0 = (tail x) : redistributeOnes' n (m-1) xs
                                | otherwise = x : redistributeOnes' n m xs

:

位> redistributeOnes [[], [1,2]]
[[1],[2]]
位> redistributeOnes [[], [1,2], [], [], [1,2,3]]
[[1],[2],[1],[],[2,3]]
位> redistributeOnes [[], [1,2], [], [1,2,3]]
[[1],[2],[1],[2,3]]
位> redistributeOnes [[]]
[[]]
位> redistributeOnes [[], [1,2,3]]
[[1],[2,3]]
位> redistributeOnes [[1,2,3]]
[[1,2,3]]
位> redistributeOnes [[1,2,3], [], [], [], [2,5,1], [1,2], [], [1,100]]
[[2,3],[1],[1],[1],[2,5,1],[2],[],[100]]
0

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


All Articles