Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:56:48+00:00 2026-05-26T06:56:48+00:00

I’m coding in SML for an assignment and I’ve done a few practice problems

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:56:49+00:00Added an answer on May 26, 2026 at 6:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.