How to delete from list, return the deleted element, and return the "modified" list

Saurabh Nanda

(Newbie alert)

I'm trying to write a function with the following signature, because I couldn't find something similar in the standard libraries:

deleteFromList :: (b -> a -> Bool) -> b -> [a] -> (Maybe a, [a])

-- Example 1: Item found in the list, and deleted
deleteFromList (\(id1, name1), (id2, name2) -> id1==id2) 3 [(1, "Jack"), (2, "Jill"), (3, "Joe"), (4, "Jimmy")]
-- returns (Just (3, "Joe"), [(1, "Jack"), (2, "Jill"), (4, "Jimmy")]

-- Example 2: Item not found in list
deleteFromList (\(id1, name1), (id2, name2) -> id1==id2) 5 [(1, "Jack"), (2, "Jill"), (3, "Joe"), (4, "Jimmy")]
-- returns (Nothing, [(1, "Jack"), (2, "Jill"), (3, "Joe"), (4, "Jimmy")]

I know this can probably be written by calling Data.List.find and Data.List.deleteBy, but that would result in two traversals of the list. I want to make my life tougher and do this in just a single traversal.

Mark Seemann

You can implement that function fairly easily with foldr, like this:

deleteFromList :: (b -> a -> Bool) -> b -> [a] -> (Maybe a, [a])
deleteFromList p target = foldr folder (Nothing, [])
  where
    folder x (Nothing, xs) | p target x = (Just x, xs)
    folder x (m, xs)                    = (m, x : xs)

although I'm sure that experienced Haskellers (which I'm not) will be able to come up with a one-liner...

Examples of usage:

*Q35115395> deleteFromList (\x y -> x == fst y) 3 [(1, "Jack"), (2, "Jill"), (3, "Joe"), (4, "Jimmy")]
(Just (3,"Joe"),[(1,"Jack"),(2,"Jill"),(4,"Jimmy")])

*Q35115395> deleteFromList (\x y -> x == fst y) 5 [(1, "Jack"), (2, "Jill"), (3, "Joe"), (4, "Jimmy")]
(Nothing,[(1,"Jack"),(2,"Jill"),(3,"Joe"),(4,"Jimmy")])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to delete doubly linked list data and return it?

From Dev

How to delete doubly linked list data and return it?

From Dev

Remove an element from list and return the result

From Dev

How to return a list of strings from a list?

From Dev

MongoDB: How to return only the last element of a list?

From Dev

Query to return list of a particular element from object list in mongo -db

From Dev

How to return a key if an element exists in List from Hash<String,List<Object>>?

From Dev

How to delete element in List<> from jsp page?

From Dev

How to delete element in List<> from jsp page?

From Dev

Return element of list as integer in Lisp

From Dev

Return element of list as integer in Lisp

From Dev

How to return a list of objects from a cfscript function

From Dev

How to return List of Object from server in GWT

From Dev

How to return a list from a method in C#

From Dev

How to Return a Generic List (Of T) from a Function

From Dev

How to return item index from list SML?

From Dev

how to return a task from execution of list of tasks

From Dev

how to correctly return std::list from dll

From Dev

How to return an array from a list [Java]

From Dev

How to add to array from list and return array

From Dev

How to return a matching item from a list in Python

From Dev

How to return a list of objects from a cfscript function

From Dev

How to Return list of bytes from webservice

From Dev

How to return a List of objects from an async task

From Dev

how to return a task from execution of list of tasks

From Dev

How to return a list from a recursive function in Python?

From Dev

How to return a matching item from a list in Python

From Dev

how to return list of result from sequelizejs to grpc?

From Dev

How to return and clear a list?

Related Related

  1. 1

    How to delete doubly linked list data and return it?

  2. 2

    How to delete doubly linked list data and return it?

  3. 3

    Remove an element from list and return the result

  4. 4

    How to return a list of strings from a list?

  5. 5

    MongoDB: How to return only the last element of a list?

  6. 6

    Query to return list of a particular element from object list in mongo -db

  7. 7

    How to return a key if an element exists in List from Hash<String,List<Object>>?

  8. 8

    How to delete element in List<> from jsp page?

  9. 9

    How to delete element in List<> from jsp page?

  10. 10

    Return element of list as integer in Lisp

  11. 11

    Return element of list as integer in Lisp

  12. 12

    How to return a list of objects from a cfscript function

  13. 13

    How to return List of Object from server in GWT

  14. 14

    How to return a list from a method in C#

  15. 15

    How to Return a Generic List (Of T) from a Function

  16. 16

    How to return item index from list SML?

  17. 17

    how to return a task from execution of list of tasks

  18. 18

    how to correctly return std::list from dll

  19. 19

    How to return an array from a list [Java]

  20. 20

    How to add to array from list and return array

  21. 21

    How to return a matching item from a list in Python

  22. 22

    How to return a list of objects from a cfscript function

  23. 23

    How to Return list of bytes from webservice

  24. 24

    How to return a List of objects from an async task

  25. 25

    how to return a task from execution of list of tasks

  26. 26

    How to return a list from a recursive function in Python?

  27. 27

    How to return a matching item from a list in Python

  28. 28

    how to return list of result from sequelizejs to grpc?

  29. 29

    How to return and clear a list?

HotTag

Archive