Last active
November 25, 2018 17:05
-
-
Save epequeno/d74085b367fb9506d5935db1d215db71 to your computer and use it in GitHub Desktop.
recursive fibo demo app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Elm 0.19.0 | |
import Html exposing (..) | |
import Html.Events exposing (..) | |
import Html.Attributes exposing (..) | |
import Browser | |
main = | |
Browser.sandbox { init = init, view = view, update = update } | |
type alias Model | |
= { input: Int | |
, output: Int | |
, previousInput: Int | |
} | |
init = | |
{ input = 0 | |
, output = 0 | |
, previousInput = 0 | |
} | |
type Msg | |
= SendInput String | |
| Submit | |
isValid model = | |
model.input < 40 | |
update msg model = | |
case msg of | |
SendInput newInput -> | |
case String.toInt newInput of | |
Just n -> | |
{ model | input = n } | |
Nothing -> | |
{ model | input = 0 } | |
Submit -> | |
if isValid model then | |
{ model | output = fibo model.input, previousInput = model.input } | |
else | |
{ model | output = 0, previousInput = 0 } | |
view model = | |
div [] [ makeOutput model | |
, br [] [] | |
, input [ placeholder "n", onInput SendInput ] [] | |
, button [ onClick Submit ] [ text "submit" ] | |
] | |
makeOutput model = | |
if isValid model then | |
text ("fibo(" ++ (String.fromInt model.previousInput) ++ ") = " ++ (String.fromInt model.output)) | |
else | |
text "Input must be less than 40!" | |
fibo n = | |
case n of | |
0 -> | |
0 | |
1 -> | |
1 | |
_ -> | |
fibo (n-1) + fibo(n-2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment