[Haskell-cafe] Stack ADT?

Casey Hawthorne caseyh at istar.ca
Thu Feb 4 12:29:17 EST 2010


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


More information about the Haskell-Cafe mailing list