7

I'm coding in SML for an assignment and I've done a few practice problems and I feel like I'm missing something- I feel like I'm using too many case statements. Here's what I'm doing and the problem statements for what I'm having trouble with.:

  1. Write a function all_except_option, which takes a string and a string list. Return NONE if the string is not in the list, else return SOME lst where lst is like the argument list except the string is not in it.

    fun all_except_option(str : string, lst : string list) =
      case lst of 
       [] => NONE
      | x::xs => case same_string(x, str) of
                   true => SOME xs
                 | false => case all_except_option(str, xs) of
                              NONE => NONE
                            | SOME y=> SOME (x::y)  
    
  2. Write a function get_substitutions1, which takes a string list list (a list of list of strings, the substitutions) and a string s and returns a string list. The result has all the strings that are in some list in substitutions that also has s, but s itself should not be in the result.

    fun get_substitutions1(lst : string list list, s : string) = 
      case lst of
        [] => []
      | x::xs => case all_except_option(s, x) of
                     NONE => get_substitutions1(xs, s)
                    | SOME y => y @ get_substitutions1(xs, s)
    

- same_string is a provided function, fun same_string(s1 : string, s2 : string) = s1 = s2

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
user998876
  • 167
  • 1
  • 1
  • 7
  • 14
    This is part of the assignment for week2 in the Coursera Programming Languages course. Since it's a violation to post solutions online, I request this question to be re-worded to change the function names so they don't match exactly with the assignment. – or9ob Oct 21 '13 at 12:30

3 Answers3

11

First of all I would start using pattern matching in the function definition instead of having a "top-level" case statement. Its basically boils down to the same thing after de-sugaring. Also I would get rid of the explicit type annotations, unless strictly needed:

fun all_except_option (str, []) = NONE
  | all_except_option (str, x :: xs) =
    case same_string(x, str) of
      true  => SOME xs
    | false => case all_except_option(str, xs) of
                 NONE   => NONE
               | SOME y => SOME (x::y)

fun get_substitutions1 ([], s) = []
  | get_substitutions1 (x :: xs, s) =
    case all_except_option(s, x) of
      NONE   => get_substitutions1(xs, s)
    | SOME y => y @ get_substitutions1(xs, s)

If speed is not of importance, then you could merge the two cases in the first function:

fun all_except_option (str, []) = NONE
  | all_except_option (str, x :: xs) =
    case (same_string(x, str), all_except_option(str, xs)) of
      (true, _)       => SOME xs
    | (false, NONE)   => NONE
    | (false, SOME y) => SOME (x::y)

But since you are using append (@), in the second function, and since it is not tail recursive, I don't believe that it your major concern. Keep in mind that append is potential "evil" and you should almost always use concatenation (and then reverse your result when returning it) and tail recursion when possible (it always is).

If you really like the explicit type annotations, then you could do it like this:

val rec all_except_option : string * string list -> string list option  =
 fn (str, []) => NONE
  | (str, x :: xs) =>
    case (same_string(x, str), all_except_option(str, xs)) of
      (true, _)       => SOME xs
    | (false, NONE)   => NONE
    | (false, SOME y) => SOME (x::y)


val rec get_substitutions1 : string list list * string -> string list =
 fn ([], s) => []
  | (x :: xs, s) =>
    case all_except_option(s, x) of
      NONE   => get_substitutions1(xs, s)
    | SOME y => y @ get_substitutions1(xs, s)

But that is just my preferred way, if I really have to add type annotations.

By the way, why on earth do you have the same_string function? You can just do the comparison directly instead. Using an auxilary function is just wierd, unless you plan to exchange it with some special logic at some point. However your function names doesn't sugest that.

Jesper.Reenberg
  • 5,944
  • 23
  • 31
  • When I try the top code block, I get compile errors saying 'Error: operator is not a function' - It seems like the case all_except_options(s, x) section can't be separated from the get_substitutions1 (x :: xs, s) block. How can I tell sml that NONE and SOME are cases for what's returned by all_except_options and not cases for get_substitutions? – Sekm Feb 03 '13 at 14:14
  • 2
    You could say that "the fun keyword breaks up the two functions". It works in SML/NJ 110.72, so either you are using another interpreter or you have done something wrong. You could put the nested case expression inside parenthesis. However the error message you are getting, doesn't sound like it has anything to do with the cases? – Jesper.Reenberg Feb 04 '13 at 23:30
  • @Jesper.Reenberg I know this is an old thread, but I'm trying to puzzle out what is in `SOME y` in `all_except_option`. If you switch it to return SOME (x::"wtf"::y) you can see that runs on all elements prior to matching the string, but not on any elements after the string is matched. If y holds the remainder of the list, why don't I see the list duplicated? I'm having a hard time 'stepping' through this problem with a pad of paper and keeping track of what SOME y actually points to at each step. – mat4nier Oct 30 '16 at 21:22
1

In addition to what Jesper.Reenberg mentioned, I just wanted to mention that a match on a bool for true and false can be replaced with an if-then-else. However, some people consider if-then-else uglier than a case statement

user102008
  • 30,736
  • 10
  • 83
  • 104
0
fun same_string( s1: string, s2: string ) = if String.compare( s1, s2 ) = EQUAL then true else false


fun contains( [],   s: string ) = false
|   contains( h::t, s: string ) = if same_string( s, h ) then true else contains( t, s )


fun all_except_option_successfully( s: string, [] )   = []
|   all_except_option_successfully( s: string, h::t ) = if same_string( s, h ) then t else ( h :: all_except_option_successfully( s, t ) )


fun all_except_option( s: string, [] )   = NONE
|   all_except_option( s: string, h::t ) = if same_string( s, h ) then SOME t else if contains( t, s ) then SOME ( h :: all_except_option_successfully( s, t ) ) else NONE
Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
Cyril
  • 1
  • 1