[Haskell-cafe] Stack ADT?
Daniel Peebles
pumpkingod at gmail.com
Thu Feb 4 17:18:34 EST 2010
>
> data Stack a = EmptyStk | Stk a (Stack a)
I find it amusing that the book defined a type that is exactly isomorphic to
the standard list (EmptyStk === [] and Stk === (:)). I guess it's just for
clarity?
On Thu, Feb 4, 2010 at 12:29 PM, Casey Hawthorne <caseyh at istar.ca> 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?
> >
> >Michael
> >
>
>
> From "Algorithms: a functional programming approach"
> Second edition
> Fethi Rabhi
> Guy Lapalme
>
>
> data Stack a = EmptyStk
> |
> Stk a (Stack a)
>
> push x s = Stk x s
>
> pop EmptyStk = error "pop from an empty stack"
> pop (Stk _ s) = s
>
> top EmptyStk = error "top from an empty stack"
> top (Stk x _) = x
>
> emptyStack = EmptyStk
>
> stackEmpty EmptyStk = True
> stackEmpty _ = False
>
>
>
>
> newtype Stack a = Stk [a]
>
> push x (Stk xs) = Stk (x:xs)
>
> pop (Stk []) = error "pop from an empty stack"
> pop (Stk (_:xs)) = Stk xs
>
> top (Stk []) = error "top from an empty stack"
> top (Stk (x:_)) = x
>
> emptyStack = Stk []
>
> stackEmpty (Stk []) = True
> stackEmpty (Stk _ ) = False
>
>
> --
> Regards,
> Casey
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100204/6456e1ca/attachment.html
More information about the Haskell-Cafe
mailing list