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

  • Home
  • SEARCH
  • 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 4025636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:52:17+00:00 2026-05-20T10:52:17+00:00

I believe I’m on the right track in converting my first function to only

  • 0

I believe I’m on the right track in converting my first function to only use assignments and loops. I know this is against functional programming, but it’s what a professor wants.

Recursive function:

fun sub (x, y, []) = []
  | sub (x, y, z::zz) = if x = z then y::sub(x, y, zz)
            else z::sub(x, y, zz);

Iterative translation:

fun sub2 (x, y, z) =
    let val ret = ref []; val temp = z;
    in
        while !temp <> []
        do (if x = hd(!temp) then ret := !ret::y; temp := tl(!temp)
            else ret := ret::hd(!temp); temp := tl(!temp));
        !ret;
    end;

I receive the following errors running on smlnj. The first on the line with the do, and second being at the end.

Error: syntax error: replacing END with EQUALOP

Error: syntax error found at EOF

I’d appreciate help debugging or perhaps a cleaner way to accomplish this iterative function.

  • 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-20T10:52:18+00:00Added an answer on May 20, 2026 at 10:52 am

    Why oh why would he ever wan’t that? Never mind…

    There are quite a few problems.

    1. You are using way to many semicolons, where they are not needed. This is however not a syntactic error.
    2. You have forgot parenthesis around your sequence (exp1; exp2) in your if-statement. It is only allowed to exclude the parenthesis in the “in” part of a let..in..end expression.
    3. You are refering to temp as a ref type (using := and !). You have however not made it a ref. This means that your input variable z will have to be given as a reference. If this is what you intend, then it doesn’t match the original sub-function.
    4. The original sub-function restricts itself to equality types. However if this was not the case, then your !temp <> null will make the restriction. It would be “best practice” to use the List.null function instead.
    5. The semicolon at the last !ret; should not be there as your sequence stops, else end will become part of the sequence which will fail.
    6. You have forgotten to dereference ret in you else part of the condition.
    7. You have switched the arguments of cons (::). Cons has type 'a * 'a list and thus takes an element and then a list of elements. One way of fixing this and still preserving the order of elements is to use the append (@) function and then placing the element to append in a singleton list. However there are so many ways of handling this in a better way as the append function performs very poorly on big lists.

    Following is a function that works:

    fun sub2 (x, y, z) =
    let 
      val ret = ref []
      val temp = ref z
    in
      while not (null (!temp)) do 
        if x = hd(!temp) then 
          (ret := !ret @ [y]; 
           temp := tl(!temp))
        else 
          (ret := !ret @ [hd(!temp)]; 
           temp := tl (!temp));
      !ret
    end
    

    One obvious thing that can be improved here is that you always update temp with the same value. So this could be factored out. And the condition could be changed to a case instead

    fun sub2 (x, y, z) =
        let 
          val ret = ref []
          val temp = ref z
        in
          while not (null (!temp)) do       
            (case x = hd(!temp) of
              true => ret := y :: !ret
            | false => ret := hd(!temp) :: !ret
           ;temp := tl (!temp));         
          rev (!ret)
        end
    

    Especially notice how the elements are not appended but to the resulting list, but placed in the front and then at the end the resulting list is reversed to get the correct order. This will give you much better performance on big lists. There are however still better ways of doing this when you go imperative style in SML.

    As you have already seen, it can be done in a functional way. But it could also be done simpler. Consider the following using map.

    fun sub3 (x, y, zs) = map (fn z => if z = x then y else z) zs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please believe me when I say I understand this isn't the right way to
I believe I know how to do this, but wanted to verify with my
I believe this is the right header: #include <cstdio> Note, there is a difference
I believe there should exist a known method for converting this: <div class=singlePane pane>
I believe several of us have already worked on a project where not only
I believe this might be one of the most common problem that users faces
I believe this has been a bug/problem in SQL 2000/2005 ... If my results
I believe I already know the answer, but I am not 100% sure, so
I believe I understand this in terms of hardware, where multiple individuals 'share' the
I believe I've seen a demonstration of this somewhere but I can't remember where.

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.