Last active
April 30, 2017 21:36
-
-
Save CliffordAnderson/040342603be0b466b997097cc06a55ee to your computer and use it in GitHub Desktop.
Fibonacci Sequence in Elm
This file contains 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
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) |
This file contains 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
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 |
This file contains 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
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