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

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.

有帮助吗?

解决方案

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')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top