Search code examples
sml

Convert Array to List in SML


How do i convert an array type to a list type in sml. I have searched the list and array structure functions but have not found one that does this (there is a list to array function though).

Description of List structure: http://sml-family.org/Basis/list.html

Description of Array structure: http://sml-family.org/Basis/array.html


Solution

  • While there isn't a built in direct conversion function, you could use Array.foldr to quite easily construct a corresponding list:

    fun arrayToList arr = Array.foldr (op ::) [] arr
    

    Example:

    - val arr = Array.fromList [6, 3, 5, 7];
    val arr = [|6,3,5,7|] : int array
    
    - val ls = arrayToList arr;
    val ls = [6,3,5,7] : int list