The right part of the sentence is not consistent with the type of function result

Write a remove_option function that takes a string and a list of strings. Returns NONE if the string is not in the list, else returns SOME xs, where xs is identical to the argument list, except that it does not have a string. You can assume that the line is in the list no more than once. Use the same_string provided to you to compare strings. An approximate solution is about 8 lines.

The type of the function must be fn: string * string list → string list option. Here is my code

fun same_string(s1 : string, s2 : string) =
    s1 = s2
fun remove_option (str: string ,str_list : string list) =
    case str_list of 
        [] => NONE
          | x::xs => if same_string(x,str) 
             then SOME xs 
             else x :: remove_option( str,xs)

and error report

hw2provided.sml:10.5-15.37 Error: right-hand-side of clause doesn't agree with f
unction result type [tycon mismatch]
  expression:  _ option
  result type:  string list
  in declaration:
    remove_option =
      (fn (<pat> : string,<pat> : string list) =>
            (case str_list
              of <pat> => <exp>
               | <pat> => <exp>))

uncaught exception Error
  raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
             ../compiler/TopLevel/interact/evalloop.sml:44.55
             ../compiler/TopLevel/interact/evalloop.sml:292.17-292.20

So where is the mistake?

+4
source share
2 answers

Since John showed where the error is, here are some additional comments:

  • same_string , . =.
  • , , , :

    fun remove_option (s1, []) = NONE
      | remove_option (s1, s2::ss) =
        if s1 = s2
        then SOME ss
        else case remove_option (s1, ss) of
                  NONE => NONE
                | SOME ss' => SOME (s2::ss')
    

    ,

    case x_opt of
         NONE => NONE
       | SOME x => SOME (f x))
    

    , , Option.map : ('a -> 'b) -> 'a option -> 'b option:

    Option.map f x_opt
    

    fun curry f x y = f (x, y)
    
    fun remove_option (s1, []) = NONE
      | remove_option (s1, s2::ss) =
        if s1 = s2
        then SOME ss
        else Option.map (curry op:: s2) (remove_option (s1, ss))
    

    curry op:: s2, , s2 .

+3

, string list option,

else x :: remove_option( str,xs)

, string list

remove_option( str,xs),

1) , , NONE

2) strings ( , ), SOME strings, x SOME .

case, .

+5

All Articles