Reputation: 381
Where can i find a good explanation on Stack in Haskell. Currently all i know is:
A traditional abstract data type completely hides the internal representation of data
Can implement a polymorphic stack using a list without telling the consumer about its inner workings.
Upvotes: 0
Views: 271
Reputation: 69984
The usual reason one would need to use an abstract stack interface is because the internal representation supports operations we would want to hide. For example, a mutable linked list or array would let people change elements in the middle of the stack if they got hold of the internal representation.
In Haskell you don't really have this problem since lists are singly-linked and immutable. The only operation you can do with them is to create a new list with the :
constructor (essentially a "push" operation) and to pattern match the list, getting back the head element and the tail of the list (essentially a "pop" operation). All other lists functions are implemented on top of these two "push and pop" primitives.
Upvotes: 7
Reputation: 842
There's a good explanation of creating a stack in Haskell using the State monad in LYAH:
http://learnyouahaskell.com/for-a-few-monads-more#state
Upvotes: 2