Sorting elm and [comparable]

What sorting algorithm is used when sorting Elm List

?

> sort [1,3,5,6]
[1,3,5,6] : [comparable]

      

What is a type [comparable]

and how do I get it back number

?

> [1,2]:: (sort [3,2,1])
[1 of 1] Compiling Repl                ( repl-temp-000.elm )
Type error on line 4, column 3 to 24:
        [1,2] :: (sort [3,2,1])

   Expected Type: [number]
     Actual Type: comparable

      

This may be a good time to understand how List

Elm is being implemented, but I'm not trying to ask for something too deep right now. Just fire it up and run it.

+3


source to share


1 answer


Sort converts the list to a javascript array and then invokes the sort and then converts the result back to an elm list. This means that the sort itself is whatever implementation is used by the browser (definitely the log size logging algorithm) (size).

Source for sorting in elms: https://github.com/elm-lang/Elm/blob/20ccc834c1a597d1ef356c14073670b62f90d875/libraries/Native/List.js#L267-L269

The type is comparable

interesting, but includes a number. The problem with your code is using (::)

(pronounced cons) rather than (++)

pronounced like append. Do you want to:



sorted = [1,2] ++ (sort [6,5,4,3])

      

Example: http://share-elm.com/sprout/53dd978ce4b07afa6f983b7d

Hope this helps!

+8


source







All Articles