ZeeeeeV
ZeeeeeV

Reputation: 381

Haskell Stack Explanation

Where can i find a good explanation on Stack in Haskell. Currently all i know is:

Upvotes: 0

Views: 271

Answers (2)

hugomg
hugomg

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

jobo3208
jobo3208

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

Related Questions