dwayne/elm-stack/1.0.0

Stack

type Stack a

A data structure that only allows adding and removing elements from the top, following the last in, first out (LIFO) principle.

Construct

The empty stack.

fromList : List a -> Stack a

Create a stack from a list.

Query

isEmpty : Stack a -> Bool
isEmpty empty == True


isEmpty (fromList []) == True


isEmpty (fromList [ 1 ]) == False
top : Stack a -> Maybe a

Get the last element added to the stack without modifying the stack.

top empty == Nothing


top (push 5 (fromList [ 4, 3, 2, 1 ])) == Just 5

Modify

push : a -> Stack a -> Stack a

Add an element to the top of the stack.

push 1 empty == fromList [ 1 ]


push 2 (fromList [ 1 ]) == fromList [ 2, 1 ]
pop : Stack a -> Maybe ( a, Stack a )

Remove and return the last element added to the stack.

pop empty == Nothing


pop (push 1 empty) == Just ( 1, empty )


pop (push 2 (fromList [ 1 ])) == Just ( 2, fromList [ 1 ] )

Convert

toList : Stack a -> List a
toList empty == []


toList (fromList [ 5, 4, 3, 2, 1 ]) == [ 5, 4, 3, 2, 1 ]