0

I need help completing this code:

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

if written correctly it would return

- insertSorted(5, fn(a, b) => a > b, [8, 6, 3, 1]);
val it = [8, 6, 5, 3, 1]

The code takes a value, a comparison function, and a list and returns a new list like the one given above. Comp is used to determine what order the values in the list should be.

  • 1
    Since you don't say what the code is supposed to do, how do you expect someone to come with any recommendation as to how to complete it? I can kind of guess that it's a sorting algorithm and maybe Dijkstra invented it. But then what? If in doubt, I'd start by filling in characters from the ASCII table. – sshine Dec 04 '19 at 17:59
  • @Simon'ReinstateMonica'Shine I added more info to the bottom. – TheMathDude92 Dec 04 '19 at 18:47

1 Answers1

1

Here's the completion in English:

  • If x should be inserted before a, the result is x::a::rest
  • Otherwise, recursively insert x into rest and add a at the front of that result.

Translating into ML left as an exercise.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82