0

I'm writing a function that uses one list as an occurrence. The function then takes this list and removes all occurrences of it from another list.

For example:

[1,2,3] should be removed from [3,2,1,2,3,1,2,3] giving us [3,2]

[1,2,3] should not be removed in the way that I would get an empty list []

So far I've got it to remove one of all the occurrences, but it won't remove any others.

Here is my function:

fun deleteAll l1 [] = []
  | deleteAll l1 (hd::tl) =
      if starts l1 (hd::tl)
      then (deleteAll l1 tl; delete l1 (hd::tl))
      else [hd]@deleteAll l1 tl;

Here are the other functons that are used within it:

fun starts [] l2 = true
  | starts l [] = false
  | starts (h1::t1) (h2::t2) = (h1=h2) andalso (starts t1 t2);

fun delete l1 [] = []
  | delete l1 (hd::tl) =
      if starts l1 (hd::tl)
      then List.drop(hd::tl, length l1)
      else [hd]@delete l1 tl;
Community
  • 1
  • 1
madcrazydrumma
  • 1,847
  • 3
  • 20
  • 38

1 Answers1

1

Before doing something advanced like that, first implement an easier case, namely a function that will delete every occurrence of a given element. Thereafter you can use this function as a tool to implement the next function, namely deleting every element occurring in one list from another.

I urge you to apply your mind and not to look at the solution. Here is more detail: SML List Deletion

Solution:

Concerning the first one you can do this:

fun del e [] = []
  | del e (h::t) =
      if e=h
      then del e t
      else h :: (del e t);

Concerning the second one, you will implement like this:

fun delete _ [] = []
  | delete [] l2 = l2
  | delete (h1::t1) t2 = delete t1 (del h1 t2);

Alternatively, you can also use a build in function such as filter in place of the first one.

Community
  • 1
  • 1
Kevin Johnson
  • 1,890
  • 13
  • 15
  • Is there any way to do it using the functions I have made beforehand? – madcrazydrumma Oct 05 '15 at 16:55
  • I am afraid not as the function `start` is only going to determine if a subset exists in a given list. How can you possibly take recourse to something like that for situations such as : `delete [4,6,5] [1,2,3,4,6,6,6,6,6,6,6,7,8,5];`? The subset [4,6,5] is non existent whereas you would like to have that every occurrence of 4, 6 and 5 is eliminated. Two different things altogether. – Kevin Johnson Oct 05 '15 at 23:25
  • No actually, I want every occurence of [4,6,5] to be deleted, not the individual elements! – madcrazydrumma Oct 05 '15 at 23:27
  • exactly what I am saying. You want `delete [4,6,5] [1,2,3,4,6,6,6,6,6,6,6,7,8,5];` to evaluate to `[1,2,3,7,8]`, so how can you ever apply a function like `start` to achieve that? – Kevin Johnson Oct 06 '15 at 00:32
  • I want `deleteAll [1,2,3] [3,2,1,2,3,2,1,2,3]` to evaluate to `[3,2,2]`... I only want to delete all the occurences of the **whole list** (subset list), not just the singular items. – madcrazydrumma Oct 06 '15 at 09:43
  • In that case, edit your question ' The function then takes this list and removes all occurrences of it from another list.' with a few examples and anti examples as currently the question is not asking what you want. – Kevin Johnson Oct 06 '15 at 14:05
  • Alright I have edited the question :) How would I use my previous functions to now solve my error correctly? – madcrazydrumma Oct 06 '15 at 14:13