# List.reverse behaves strange

**URL:** <https://discourse.elm-lang.org/t/list-reverse-behaves-strange/5030>\
**Category:** Learn\
**Created:** [January 19, 2020, 8:56pm UTC](https://discourse.elm-lang.org/t/list-reverse-behaves-strange/5030 "2020-01-19T20:56:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexandru-calinoiu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alexandru-calinoiu/32/2881_2.png) [@alexandru-calinoiu](https://discourse.elm-lang.org/u/alexandru-calinoiu)\
**Post date:** [January 19, 2020, 8:56pm UTC](https://discourse.elm-lang.org/t/list-reverse-behaves-strange/5030/1 "2020-01-19T20:56:23Z")

</div>

Hi,

Trying out elm, I stumbled about this issue

```auto
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?

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [January 19, 2020, 10:01pm UTC](https://discourse.elm-lang.org/t/list-reverse-behaves-strange/5030/2 "2020-01-19T22:01:55Z")

</div>

> [@alexandru-calinoiu](#):
>
> remainderBy 10 n :: digits (n // 10)

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.

---

<div class="post-metadata">

**Author:** ![alexandru-calinoiu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alexandru-calinoiu/32/2881_2.png) [@alexandru-calinoiu](https://discourse.elm-lang.org/u/alexandru-calinoiu)\
**Post date:** [January 20, 2020, 6:12am UTC](https://discourse.elm-lang.org/t/list-reverse-behaves-strange/5030/3 "2020-01-20T06:12:25Z")

</div>

Indeed, thank you for the clarification,

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [January 30, 2020, 6:12am UTC](https://discourse.elm-lang.org/t/list-reverse-behaves-strange/5030/4 "2020-01-30T06:12:25Z")

</div>

Closed on January 30, 2020, 6:12am UTC

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