In SML, how do I remove the first occurence of a pattern from a list and then return the removed item and the rest of the list? [closed]

StackOverflow https://stackoverflow.com/questions/19587622

Question

I'm trying to write an SML function that takes as parameters a list and an item. If the item is present, the function should return a tuple containing the list without the first such occurence of that item and the item just removed. If there are no occurences of the item in the list, the function should return NONE or something similar to indicate this absence.

Was it helpful?

Solution

Try this:

fun same_string(str, lst) =
        case lst of
             []     =>   NONE
            |x::xs    =>  case same_string(str, xs) of
                            NONE   => if str = x  
                                      then SOME(xs)
                                      else NONE
                           |SOME xs' => SOME (x :: xs')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top