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 6989169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:10:37+00:00 2026-05-27T19:10:37+00:00

I´m a newbie in SML and I´d like to update my function so that

  • 0

I´m a newbie in SML and I´d like to update my function so that it has two outputs: a list AND 1 or 0. The function was proposed here: SML: Remove the entry from the List. It returns an updated list without a row that contains ´elem´.

fun removeElem elem myList = filter (fn x => x <> elem) myList

The function should return a new list AND 1, if a raw has been deleted. Otherwise, it should return an old list AND 0.

Any hint or example is highly appreciated. Thanks.

  • 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-27T19:10:37+00:00Added an answer on May 27, 2026 at 7:10 pm

    Note that all SML functions take a single input and return a single output. Instead, think of returning a tuple containing the new list and a flag indicating whether any elements were removed. One possibility is to use a couple of functions from the standard basis to test whether elem is in myList and build up a tuple consisting of that and the results from the filter shown in the question. The test might look like:

    Option.isSome (List.find (fn x => x = elem) myList)
    

    There are more concise ways to write that, but it shows the idea. Note that it returns a bool instead of an int; this is more precise, so I won’t convert to the integers requested in the question.

    A drawback of the above is that it requires traversing the list twice. To avoid that, consider the type that the function must return: a tuple of a list without elem and a flag showing whether any elems have been removed. We can then write a function that take a new value and a (valid) tuple, and returns a valid tuple. One possibility:

    fun update(x, (acc, flag)) = if x = elem then (acc, true) else (x :: acc, flag)  
    

    We can then apply update to each element of myList one-by-one. Since we want the order of the list to stay the same, apart from the removed elements, we should work through myList from right to left, accumulating the results into an initially empty list. The function foldr will do this directly:

    foldr update ([], false) myList
    

    However, there is a lot of logic hidden in the foldr higher-order function.

    To use this as a learning exercise, I’d suggest using this problem to implement the function in a few ways:

    • as a recursive function
    • as a tail-recursive function
    • using the higher order functions foldl and foldr

    Understanding the differences between these versions will shed a lot of light on how SML works. For each version, let the types guide you.

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

Sidebar

Related Questions

Newbie here. The following function works fine when $color refers to an entry in
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie C++ programmer here. I'm trying to write a command line application that takes
Newbie here. I am looking at company code. It appears that there are NO
Newbie to .NET data apps here, coming from a Visual Foxpro background. I'm planning
Newbie question here! I'm building a simple application that allows users to create and
Newbie question... So I have two activities, Cherry and Apple, and each one has
Newbie SQL question here --> I've got an Occurrences table that contains a row
Newbie question here, is there any inbuilt PHP tag that can be used to
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier

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.