, . :
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]]