Created
January 14, 2016 03:59
-
-
Save chikamichi/11ca7c5dfd0014a9a4f2 to your computer and use it in GitHub Desktop.
Elm - reworking the FIFO implementation from
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
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