# ELM recursive iteration constructor

**URL:** <https://discourse.elm-lang.org/t/elm-recursive-iteration-constructor/7599>\
**Category:** Learn\
**Created:** [July 28, 2021, 10:34am UTC](https://discourse.elm-lang.org/t/elm-recursive-iteration-constructor/7599 "2021-07-28T10:34:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [July 28, 2021, 10:34am UTC](https://discourse.elm-lang.org/t/elm-recursive-iteration-constructor/7599/1 "2021-07-28T10:34:58Z")

</div>

I have a function `computeNextValue` such as for a given number x, it computes the number y, by additioning all digits of number x to itself  
ex : Given x = 45: y = 54 = 4 + 5 + 45

computeNextValue : Int → Int  
computeNextValue x =  
List.sum (numberToList x) + x

and i want to mplement the function `computeNextValueWithIteration` with the following signature :

computeNextValueWithIteration : Int → Int → Int

The first parameter will be an “iteration” value, the second parameter will be the “number” value such that I compute “iteration” number of times to get the result y.

ex : Given x = 123 and iteration = 3, you would compute why like this :

- Iteration 1 : 129 = 1 + 2 + 3 + 123
- Iteration 2 : 141 = 1 + 2 + 9 + 129
- Iteration 3 : 147 = 1 + 4 + 1 + 141

y = 147

```elm
my code
-- The name of the module must always be the same name as the file !
module Main exposing (main)

{-
  All imports we need, nothing more to add for this quest
  Checkout https://elm-lang.org/docs and https://package.elm-lang.org/ when needed
-}
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onSubmit, onInput)
import String exposing (fromInt)
import Basics
import Array exposing (Array)

type alias Model =
  { input : Maybe Int
  , output : Maybe Int
  }

-- We initialize the default value of our model
init : Model
init =
  { input = Nothing
  , output = Nothing
  }

-- We render a view given the model
view : Model -> Html Msg
view model =
  div []
  [input [ type_ "number", placeholder "Enter a number", value (maybeIntToString model.input), onInput SetValue] []
  , br [] []
  , button [onClick SubmitForm, id "compute"] [text "Calculate"]
  , div [] [text "output : ", span [id "output"] [text <| maybeIntToString model.output]]
  ]

-- Helper function
maybeIntToString : Maybe Int -> String
maybeIntToString model =
  case model of
    Nothing -> ""
    Just v -> fromInt v

type Msg
  = NoOp
  | SubmitForm
  | SetValue String

update : Msg -> Model -> Model
update msg model =
  -- With Debug.log we log into the browser console, see https://package.elm-lang.org/packages/elm-lang/core/3.0.0/Debug
  case Debug.log "msg" msg of
    NoOp ->
      model
    SetValue v ->
      { model | input = String.toInt v }
    SubmitForm ->
      { model | output = Maybe.map computeNextValue model.input }

-- To implement
computeNextValue : Int -> Int
computeNextValue x =
   List.sum (numberToList x) + x

numberToList : Int -> List Int
numberToList number =
    String.fromInt number -- convert our Int to a String
        |> String.split ""
        |> List.filterMap String.toInt

computeNextValueWithIteration : Int -> Int -> Int
computeNextValueWithIteration n x =
    List.foldl (\_ sum -> computeNextValue sum) x (List.repeat n 0)

main : Program () (Model) Msg
main =
  Browser.sandbox({ init = init, view = view, update = update})

```

i want to con sider this steps

- Add an `iteration` field of type `Int` to the model
- Initialize it to the value 0 where required
- Add the data constructor `SetIteration String` to the `Msg` type
- Add the following code to the `update` function

```auto
SetIteration v ->
{ model | iteration = Maybe.withDefault 0 <| String.toInt v}

```

- Modify the `update` function on the `SubmitForm` case to use the function `computeNextValueWithIteration `instead of the` computeNextValue` implemented on quest 1
- Modify the `view` function to add an `input` (html) element of type `number` with the id `iteration` . Do not forget to set the **onInput** event correctly. Use the imported function `fromInt` to convert the integer to string

---

<div class="post-metadata">

**Author:** ![grigorious](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/grigorious/32/3549_2.png) [@grigorious](https://discourse.elm-lang.org/u/grigorious)\
**Post date:** [July 28, 2021, 11:52am UTC](https://discourse.elm-lang.org/t/elm-recursive-iteration-constructor/7599/2 "2021-07-28T11:52:23Z")

</div>

Something like this: [iterations](https://ellie-app.com/dRfQR3v26Rva1) ?

```elm
module Main exposing (main)

import Browser
import Html exposing (Html, button, div, text)
import Html.Attributes as Attr
import Html.Events exposing (onInput, onSubmit)

type alias Model =
    { iterations : Int
    , input : Int
    , output : Int
    }

initialModel : Model
initialModel =
    { iterations = 0
    , input = 0
    , output = 0
    }

type Msg
    = Iterations String
    | Input String
    | Compute

update : Msg -> Model -> Model
update msg model =
    case msg of
        Iterations iterations ->
            { model | iterations = Maybe.withDefault 0 <| String.toInt iterations }

        Input input ->
            { model | input = Maybe.withDefault 0 <| String.toInt input }

        Compute ->
            { model | output = computeNextValueWithIteration model.iterations model.input}

computeNextValueWithIteration : Int -> Int -> Int
computeNextValueWithIteration iterations input =
    case iterations of
        0 -> input
        _ -> computeNextValueWithIteration (iterations - 1) (computeNextValue input)

computeNextValue : Int -> Int
computeNextValue input =
    String.fromInt input
        |> String.split ""
        |> List.filterMap String.toInt
        |> List.sum
        |> (+) input

view : Model -> Html Msg
view model =
    Html.form [onSubmit Compute]
        [ Html.label
            [Attr.for "iterations"]
            [Html.text "# iterations"]
        , Html.input
            [ Attr.type_ "number"
            , Attr.id "iterations"
            , Attr.value <| String.fromInt model.iterations
            , onInput Iterations
            ]
            []
        , Html.label
            [Attr.for "number_input"]
            [Html.text "input"]
        , Html.input
            [ Attr.type_ "number"
            , Attr.id "number_input"
            , Attr.value <| String.fromInt model.input
            , onInput Input
            ]
            []
        , Html.button [] [Html.text "compute"]
        , Html.text <| String.fromInt model.output
        ]

main : Program () Model Msg
main =
    Browser.sandbox
        { init = initialModel
        , view = view
        , update = update
        }

```

---

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [July 28, 2021, 12:11pm UTC](https://discourse.elm-lang.org/t/elm-recursive-iteration-constructor/7599/5 "2021-07-28T12:11:44Z")

</div>

Thank youu very much

---

<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:** [August 10, 2021, 10:55am UTC](https://discourse.elm-lang.org/t/elm-recursive-iteration-constructor/7599/7 "2021-08-10T10:55:03Z")

</div>

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