Sorting List with OCaml standard library function

daniele3004

I'm studying OCaml and and doing various exercises on ordering data. I would like to understand how to use the standard librari List for ordering

For example I would like to sort this array using these functions [94; 50; 6; 7; 8; 8]

List.sort 
List.stable_sort 
List.fast_sort 
List.unique_sort

What is the syntax to do it ?

cago

If you want to use these functions on your list, you have to specifiy the comparison function.

Quote from the documentation:

The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller

In the module Pervasives you have a polymorphic comparison function:

val compare : 'a -> 'a -> int

So, in your case you can just do:

List.sort compare [94; 50; 6; 7; 8; 8]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ocaml 'a list list function tuples

From Dev

Questions about the standard library list

From Dev

OCaml function: replace a element in a list

From Dev

Haskell standard library function for transformations

From Dev

main function already defined in ocaml asmrun library

From Dev

main function already defined in ocaml asmrun library

From Dev

Standard library function for running a function only once

From Dev

What sorting algorithm does Swift implement for its standard library?

From Dev

Recursive sorting function for list in Python

From Dev

OCaml - a function which returns all the prefixes of a list

From Dev

Ocaml: function cannot return complete resulting list

From Dev

OCaml - function calling for each element in a list

From Dev

function returns list in reverse order in OCaml

From Dev

Member function pointer issue with standard library methods

From Dev

Is there a standard library function that reverses STL stacks?

From Dev

Show function status using Python standard library

From Dev

Probability Density Function using the standard library?

From Dev

If a standard library function is reimplemented, which of the two is called?

From Dev

Is there a pure virtual function in the C++ Standard Library?

From Dev

Is there a standard library function that reverses STL stacks?

From Dev

If a standard library function is reimplemented, which of the two is called?

From Dev

C standard library function 'strncpy' not working

From Dev

Library function to find difference between two lists - OCaml

From Dev

How to facilitate OCamljava compilation of ocaml library with C function calls?

From Dev

Sorting an array Imperative ocaml

From Dev

Redirect standard output OCaml

From Dev

Difference between List.iter and List.map function in OCaml

From Dev

Doubly Linked List sorting function in C

From Dev

Function sorting list by 3 columns and initiating it with a modification

Related Related

  1. 1

    ocaml 'a list list function tuples

  2. 2

    Questions about the standard library list

  3. 3

    OCaml function: replace a element in a list

  4. 4

    Haskell standard library function for transformations

  5. 5

    main function already defined in ocaml asmrun library

  6. 6

    main function already defined in ocaml asmrun library

  7. 7

    Standard library function for running a function only once

  8. 8

    What sorting algorithm does Swift implement for its standard library?

  9. 9

    Recursive sorting function for list in Python

  10. 10

    OCaml - a function which returns all the prefixes of a list

  11. 11

    Ocaml: function cannot return complete resulting list

  12. 12

    OCaml - function calling for each element in a list

  13. 13

    function returns list in reverse order in OCaml

  14. 14

    Member function pointer issue with standard library methods

  15. 15

    Is there a standard library function that reverses STL stacks?

  16. 16

    Show function status using Python standard library

  17. 17

    Probability Density Function using the standard library?

  18. 18

    If a standard library function is reimplemented, which of the two is called?

  19. 19

    Is there a pure virtual function in the C++ Standard Library?

  20. 20

    Is there a standard library function that reverses STL stacks?

  21. 21

    If a standard library function is reimplemented, which of the two is called?

  22. 22

    C standard library function 'strncpy' not working

  23. 23

    Library function to find difference between two lists - OCaml

  24. 24

    How to facilitate OCamljava compilation of ocaml library with C function calls?

  25. 25

    Sorting an array Imperative ocaml

  26. 26

    Redirect standard output OCaml

  27. 27

    Difference between List.iter and List.map function in OCaml

  28. 28

    Doubly Linked List sorting function in C

  29. 29

    Function sorting list by 3 columns and initiating it with a modification

HotTag

Archive