Using higher-order functions
Once
you have defined a higher order function like mergesort, you can use it
to sort lists in any manner you choose, as long as you can specify an
appropriate binary comparison function.
For example, you could define fun lessThan(a, b) = a < b;, and use it sort a list in ascending order by calling the higher-order mergesort like this: mergesort(lessThan, [3,2,1,45,23,2,34,12,32,22]);
Or, you could define a function greaterThan(a, b) = a > b; and use it sort a list in descending order by calling the higher-order
mergesort like this: mergesort(greaterThan, [3,2,1,45,23,2,34,12,32,22]);
Or you could define a function like this fun mystery (a, b) = if a mod 2 <> b mod 2 then a mod 2 = 0 else a < b; and use it sort a list by calling the higher-order
mergesort like this: mergesort(mystery, [3,2,1,45,23,2,34,12,32,22]); What sort order does this produce?
Note that functions like lessThan and greaterThan can be directly expressed in ML using the op keyword, as in op < and op >.