[Haskell-beginners] Writing a custom pop function for a stack data type

Emanuel Koczwara poczta at emanuelkoczwara.pl
Tue Mar 12 13:46:08 CET 2013


Hi,

Dnia 2013-03-12, wto o godzinie 11:06 +0000, doaltan pisze:
> Actually I'm getting the error with this :
> 
> 
> data Stack = Empty | Elem Char Stack deriving Show
> 
> 
> pophead :: Stack -> Char
> pophead Empty = Empty 
> pophead (Elem x stack) = x
> 
> 

  pophead should return Char. You can't return Stack if you defined the
type of pophead as Stack -> Char. You can try to use Maybe here and
return Maybe Char (Just x or Nothing) or you can use error function to
raise an error.

  You can use _ instead of stack like this:

pophead (Elem x _) = x

Emanuel






More information about the Beginners mailing list