0

I am a bit stuck with this problem in SML / SMLNJ and I would love some guidance. So I have a problem where I need to make a function called insertSorted, where it takes a number, a comparison statement, and an (assumed sorted) list that it needs to insert into. I'm not sure how to start approaching this so any help would be amazing.

My thought is to split the two lists up where the number would be, insert the number, and then concatenate both lists.

fun insertSorted (x, comp, []) = [x] 
  |  insertSorted (x, comp, a::rest) = ...

Update: I got a bit farther now I just need to know how to debug this, any guidance?

fun insertSorted (x, []) = [x]
 |  insertSorted (x, y::ys) = 
    if (x < y)
        then x::y::ys
    else if (x > y) 
        then y::x::ys
    else y::insertSorted (x, ys);

Update 2: My new goal is to figure out how to merge these two functions into one. Ultimately named insertSorted.

fun insertSorted (x, nil) = [x]
 |  insertSorted (x,y::ys) = if x<y then x::y::ys else y :: insertSorted (x,ys);

fun insertSorted (x, nil) = [x]
 |  insertSorted (x,y::ys) = if x>y then y::x::ys else y :: insertSorted (x,ys);

1 Answers1

1

There are three cases:

  1. The list is nil.
    • You've already covered this. :-)
  2. The list is not nil, and its first element is less than x, so we need to keep searching for where to insert x.
    • In this case, the result should be the first element, followed by the result of inserting x into the rest of the list.
  3. The list is not nil, and its first element is greater than or equal to x, so we can insert x right here.
    • In this case, the result should be x, followed by the entire list.

Distinguishing cases #2 and #3 involves if/then/else; implementing case #2 involves recursion.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Thank you so much for taking the time to answer! One question I have is how could I pass a comparison. The code I was given to test it is as follows: 'insertSorted(5, fn(a, b) => a < b, [8, 6, 5, 3, 1]);' – Mason Garcia Dec 12 '20 at 00:15
  • 1
    @MasonGarcia: I'm sorry, I don't understand your question. What do you mean by "how could I pass a comparison"? – ruakh Dec 12 '20 at 00:19
  • So the goal of the insertSorted function is to take a number (to insert), a comparative statement (i.e. a < b), and a list to insert to. Im having trouble inserting the comparative statement into my code. – Mason Garcia Dec 12 '20 at 00:21
  • I'm sorry for tagging you so much, I'm still getting used to stack overflow – Mason Garcia Dec 12 '20 at 00:47
  • 1
    Your test code shows exactly how to do this. `fn(a, b) => a < b` is a function that takes a pair of values and returns whether the first value is less than the second. (By the way, it's a *function*, or *function expression*, not a *statement*. Standard ML doesn't actually have "statements", but its closest analogue would be a "declaration", which this isn't.) – ruakh Dec 12 '20 at 00:58
  • Thank you for the clarification, I've been struggling a lot with terminology! I am going to post an edit: update 2. My new question now is how can I do 2 comparative functions within the same 'main' function? – Mason Garcia Dec 12 '20 at 01:02
  • @MasonGarcia: Oh, I think I understand -- your question is about how to *call* the comparison function? You can write e.g. `comp (x, y)`. – ruakh Dec 12 '20 at 01:09
  • @rukah, I can't seem to find any documentation on the comp function. Any idea how to implement it into my code? – Mason Garcia Dec 12 '20 at 02:28
  • 1
    @MasonGarcia: Consider the function `fun double i = 2 * i`, which takes an integer and returns double it (so e.g. `double 3` is `6`). You won't find any documentation on the integer `i`, because it's not a specific integer; it's just a function parameter, and can be *any* integer. Likewise, in your case `comp` isn't a specific function; it's just a function parameter, and can be *any* function of type `int * int -> bool`. – ruakh Dec 12 '20 at 05:50
  • 1
    (If this concept is new to you and you're having difficulty making sense of it, check out ["First-class function" on Wikipedia](https://en.wikipedia.org/wiki/First-class_function).) – ruakh Dec 12 '20 at 05:53