Search code examples
functional-programmingsmlnjml

How to use LIST_SORT function in SML/NJ?


I don't get how to use properly the function to sort a list in SML/NJ (Standard ML of New Jersey).

This is the manual : here

make a use case example, e.g. sort ([1,9,3,4]); in order to get [1,3,4,9].


Solution

  • Very briefly, following is the syntax:

    ListMergeSort.sort (fn(x,y)=> x>y) [3,5,6,7,4,3,7,9,1,2,3];
    

    Explanation: ListMergeSort: Because that is the structure provided as it comes in the documentation:

    Synopsis

    signature LIST_SORT

    structure ListMergeSort : LIST_SORT

    The LIST_SORT signature specifies an interface for the applicative sorting of lists.

    Thereafter, the sort function requires two parameters as is evident from the interface:

    val sort : (('a * 'a) -> bool) -> 'a list -> 'a list 
    
    1. a function that accepts two parameters and returns a boolean: (('a * 'a) -> bool) This is exemplified by the anonymous function I defined on the fly:

      fn(x,y)=> x>y

    It accepts two parameters and it returns a boolean. These two parameters are provided by the sort function who will pass in the elements of the list to be sorted.

    1. a list that requires to be sorted, e.g.: [3,5,6,7,4,3,7,9,1,2,3]