Skip to content

Instantly share code, notes, and snippets.

@chikamichi
Created January 14, 2016 03:59
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 chikamichi/11ca7c5dfd0014a9a4f2 to your computer and use it in GitHub Desktop.
Save chikamichi/11ca7c5dfd0014a9a4f2 to your computer and use it in GitHub Desktop.
Elm - reworking the FIFO implementation from
import Graphics.Element exposing (..)
import Text
type Fifo a
= Empty | Node a (List a)
empty : Fifo a
empty =
Empty
insert : a -> Fifo a -> Fifo a
insert a fifo =
case fifo of
Empty ->
Node a []
Node front back ->
Node front (a :: back)
remove : Fifo a -> ( Maybe a, Fifo a )
remove fifo =
case fifo of
Empty ->
( Nothing, empty )
Node front [] ->
( Just front, empty )
Node front (next :: rest) ->
( Just front, Node next rest )
fromList : List a -> Fifo a
fromList list =
case list of
(first :: rest) ->
Node first rest
[] ->
empty
toList : Fifo a -> List a
toList fifo =
case fifo of
Empty ->
[]
Node front back ->
[front] ++ List.reverse back
main =
empty
|> insert 9
|> insert 8
|> remove |> snd
|> insert 7
|> remove |> fst
|> show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment