Why Haskell's Data.List.deleteBy takes in input a comparison function (a -> a -> Bool) and a value instead of a predicate (a -> Bool)? -
i have question related data.list , signature of deleteby. ideally function should take in input predicate , delete first element predicate true. like:
deleteby :: (a -> bool) -> [a] -> [a] deleteby p = go go [] = [] go (x:xs) | p x = xs | otherwise = x:go xs
instead function defined in library takes both predicate , value:
deleteby :: (a -> -> bool) -> -> [a] -> [a] deleteby _ _ [] = [] deleteby eq x (y:ys) = if x `eq` y ys else y : deleteby eq x ys
it's easy see eq
used x
first argument , x
fixed in deleteby
, there no reason both eq
, x
instead of eq x
. on contrary, taking predicate working on single element can pass predicates don't compare 2 values, such function works on part of type a
or trivial function cons true
. question is: why deleteby
has been implemented in way?
the deleteby
function generalization of delete
, it's helpful take @ delete
first.
delete :: eq => -> [a] -> [a]
delete
takes value eq => a
, deletes first occurance of value [a]
using (==)
eq
instance.
as *by
functions in data.list, eq
constraint removed , programmer required provide own replacement (==)
function.
so removing eq
constraint delete
, replacing type of (==)
, namely a -> -> bool
, gives type of deleteby
.
in other words, it's consistency rest of *by
operations in data.list
.
Comments
Post a Comment