[Haskell-cafe] Stack ADT?
ok at cs.otago.ac.nz
Thu Feb 4 20:43:16 EST 2010
On Feb 5, 2010, at 6:38 AM, Casey Hawthorne wrote:
> On Thu, 4 Feb 2010 09:07:28 -0800 (PST), you wrote:
>> Can't find a Stack datatype on Hoogle? Where should I look?
>> module Stack(Stack,push,pop,top,emptyStack,stackEmpty) where
Or just use a list.
A more Haskellish approach to stacks than using "pop" and "top"
would be something like this:
newtype Stack t = [t]
emptyStack = Stack 
stackPush (Stack s) = Stack (x:s)
viewStack (Stack ) = Nothing
viewStack (Stack (top:rest)) = Just (top, Stack rest)
if stackEmpty s then ... else ... top s ... pop s ...
case viewStack s of
Nothing -> ...
Just (top, rest) -> ...
Note that none of the functions in this interface can fail.
More information about the Haskell-Cafe