Writing a custom pop function for a stack data type

Hi I have such a stack data structure: datamystack =Empty |Elem Char mystack derivingShow I'm trying to get the head of the stack using this: pophead :: Stack -> Char pophead Empty = Empty pophead (Element x stack) = x And I'm getting this error for the last sentence of the function : Not in scope: data constructor `Stack' Can you tell me how to fix it? Thanks.

You have declared new data type mystack not Stack, so haskell compiler
could not find Stack data type and its constructors. Secondly data type in
Haskell need to be start with capital letters like
data Mystack = Empty | Elem Char Mystack deriving Show
then correct Function definition is
pophead :: Mystack -> Char
Regards
Divyanshu
On Tue, Mar 12, 2013 at 4:12 PM, doaltan
Hi I have such a stack data structure:
data mystack = Empty | Elem Char mystack deriving Show
I'm trying to get the head of the stack using this: pophead :: Stack -> Char pophead Empty = Empty pophead (Element x stack) = x And I'm getting this error for the last sentence of the function :
Not in scope: data constructor `Stack' Can you tell me how to fix it? Thanks.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
________________________________
From: divyanshu ranjan
datamystack =Empty |Elem Char mystack derivingShow
I'm trying to get the head of the stack using this: pophead :: Stack -> Char pophead Empty = Empty pophead (Element x stack) = x And I'm getting this error for the last sentence of the function : Not in scope: data constructor `Stack' Can you tell me how to fix it? Thanks.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tue, Mar 12, 2013 at 11:06:35AM +0000, doaltan wrote:
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
This code will result in a type error, but not the one you said. Try compiling this exact code and see what error you get. -Brent
________________________________ From: divyanshu ranjan
To: doaltan ; The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Sent: Tuesday, 12 March 2013, 12:53 Subject: Re: [Haskell-beginners] Writing a custom pop function for a stack data type You have declared new data type mystack not Stack, so haskell compiler could not find Stack data type and its constructors. Secondly data type in Haskell need to be start with capital letters like
data Mystack = Empty | Elem Char Mystack deriving Show then correct Function definition is pophead :: Mystack -> Char
Regards Divyanshu
On Tue, Mar 12, 2013 at 4:12 PM, doaltan
wrote: Hi I have such a stack data structure:
datamystack =Empty |Elem Char mystack derivingShow
I'm trying to get the head of the stack using this: pophead :: Stack -> Char pophead Empty = Empty pophead (Element x stack) = x And I'm getting this error for the last sentence of the function : Not in scope: data constructor `Stack' Can you tell me how to fix it? Thanks.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
participants (4)
-
Brent Yorgey
-
divyanshu ranjan
-
doaltan
-
Emanuel Koczwara