Opened 8 years ago

Last modified 11 months ago

#3399 patch proposal

Generalize the type of Data.List.{deleteBy, deleteFirstsBy}

Reported by: iago Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 6.10.4
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D2526
Wiki Page:


Better (more general) type signatures would be

deleteBy :: (b -> a -> Bool) -> b -> [a] -> [a]
deleteFirstsBy :: (b -> a -> Bool) -> [b] -> [a] -> [a]

Example of why it is useful:

deleteBy ((==) . fst) 1 [(1,'a'), (2, 'b')]

Change History (10)

comment:1 Changed 8 years ago by iago

Sorry, the example was wrong, it should be

deleteBy (\x -> (x ==) . fst) 1 [(1,'a'), (2, 'b')]


deleteBy ((==) . fst) (1,'a') [1,2]

comment:2 Changed 8 years ago by maeder

Your proposal shows to me that there is another generalization of delete (I think a better one than deleteBy), let's call it deleteFirst which is a variant of filter with a negated predicate, that stops immediately after one element has been removed.

deleteFirst :: (a -> Bool) -> [a] -> [a]
deleteFirst p l =
  let (ft, rt) = break p l
  in ft ++ drop 1 rt

delete :: Eq a => a -> [a] -> [a]
delete a = deleteFirst (== a)

(In the type signature of your above function deleteFirstsBy the argument lists '[a]' and '[b]' should be swapped, because the second argument is taken away from the first one.)

Maybe a better alternative for deleteFirstsBy would be deleteFirsts:

deleteFirsts :: [a -> Bool] -> [a] -> [a]
deleteFirsts fs l = foldr deleteFirst l fs

deleteFirstsBy :: (b -> a -> Bool) -> [a] -> [b] -> [a]
deleteFirstsBy eq l r = deleteFirsts (map eq r) l

But, since hardly anybody wants to touch Data.List, I would vote for closing this ticket

comment:3 Changed 8 years ago by iago

The proposal doesn't require change code itself, it is just use the more general type signature instead of restrict it unnecessary. The change wouldn't break any code.

comment:4 Changed 8 years ago by maeder

Another argument against this proposal is, that an "equality" function taking different argument types is rather unintuitive. Who actually needs this generalization and is able to understand this from the documentation? Orthogonality would also require to check if the other "By"-functions can be generalized to swallow an "(b -> a -> Bool)" argument. (Indeed intersectBy could be generalized, too.)

comment:5 Changed 8 years ago by igloo

difficulty: Unknown
Milestone: Not GHC

comment:6 Changed 7 years ago by igloo

Resolution: wontfix
Status: newclosed
Type of failure: None/Unknown

Looks like an abandoned proposal

comment:7 Changed 14 months ago by mpickering

Differential Rev(s): Phab:D2526
Milestone: Not GHC8.2.1
Resolution: wontfix
Status: closednew

comment:8 Changed 14 months ago by mpickering

Status: newpatch

comment:9 Changed 13 months ago by bgamari

There was recently a discussion on the libraries list but it's not clear to me what the conclusion was.

comment:10 Changed 11 months ago by bgamari

Milestone: 8.2.1

This won't happen for 8.2.

Note: See TracTickets for help on using tickets.