Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active April 30, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CliffordAnderson/040342603be0b466b997097cc06a55ee to your computer and use it in GitHub Desktop.
Save CliffordAnderson/040342603be0b466b997097cc06a55ee to your computer and use it in GitHub Desktop.
Fibonacci Sequence in Elm
fib x =
case x of
0 -> 1
1 -> 1
_ -> fib (x-1) + fib (x-2)
ml x =
List.range 1 x
fibs x =
List.map fib (ml x)
import Html exposing (text)
import Html.App exposing (beginnerProgram)
main =
beginnerProgram { model = model, view = view, update = update }
model = 10
view model =
text (toString (fib model))
fib x =
case x of
0 -> 1
1 -> 1
_ -> fib (x-1) + fib (x-2)
update model =
model
import Html exposing (text, br, button, div, h2, input, p)
import Html.App exposing (beginnerProgram)
import Html.Events exposing (onClick)
main =
beginnerProgram { model = model, view = view, update = update }
model = 0
type Msg = Increment | Decrement
view model =
div [] [
h2 [] [text "Fibonnaci"],
br [] [],
button [onClick Increment] [text "+"],
br [] [],
text (toString (model)),
br [] [],
button [onClick Decrement] [text "-"],
p [] [
text ("Fibonacci Number is " ++ toString(fib model))
]
]
fib x =
case x of
0 -> 1
1 -> 1
_ -> fib (x-1) + fib (x-2)
update msg model =
case msg of
Increment -> model + 1
Decrement -> if model == 0 then model else model - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment