Make a function which returns the original list except the argument

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2013-10-26T09:19:33Z Indexed on 2013/10/26 15:54 UTC
Read the original article Hit count: 322

I want make a function which takes a list of string and a string and returns NONE if there is no string in the string list, otherwise it returns SOME of the list of string which is the same as the original list of string except it doesn't contain the initial string (pattern):

fun my_function (pattern, source_list) =
  case source_list 
    of [] => NONE
    | [x] => if pattern = x then SOME [] else NONE
    | x::xs => 
      if pattern = x 
      then SOME (xs) 
      else SOME (x) :: my_function (pattern, xs) (* this is wrong, what to do here?*)


val a = my_function ("haha", ["12", "aaa", "bbb", "haha", "ccc", "ddd"]) (* should be SOME ["12", "aaa", "bbb", "ccc", "ddd"]*)  
val a2 = my_function ("haha2", ["123", "aaa", "bbb", "haha", "ccc"]) (*should be NONE*)
val a3 = my_function ("haha3", ["haha3"]) (* should be SOME []*)

I'm confused by the 3rd case: x::xs => .... What should do there? Note that I'd like not to use any sml library function.

© Stack Overflow or respective owner

Related posts about recursion

Related posts about functional-programming