List.reverse behaves strange

Hi,

Trying out elm, I stumbled about this issue

module RippleCarryAdder exposing (digits)


digits : Int -> List Int
digits number =
    let
        getDigits n =
            if n == 0 then
                []

            else
                remainderBy 10 n :: digits (n // 10)
    in
    getDigits number
        |> List.reverse

digits 1000
[ 0, 1, 0, 0 ]

This is not what I’ve expected from the output, can someone please explain?

I guess you’re reversing the list multiple times. Try replacing that line with remainderBy 10 n :: getDigits (n // 10) to make getDigits a recursive function on its own.

1 Like

Indeed, thank you for the clarification,

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.