Please tell me what is the problem?
data Stack' v = Stack' [v] Int deriving (Show) ... type StackInt = Stack' Int main = print(StackInt [1,2,3] 4)
The error i am getting is
Not in scope: data constructor `Stackint'
What is wrong?
It looks to me like you are confusing the concepts of types and constructors, this is a common problem as they live in separate namespaces and are often given the same name. In the Haskell expression
data SomeType = SomeType Int
say, you are actually defining the type SomeType
and a constructor SomeType
. The type is not a function in the normal sense, but the constructor is. If you asked ghci for the type of SomeType you would get this:
:t SomeType
SomeType :: Int -> SomeType
Now, a type
declaration is just a shorthand for a longer type definition, in your case making StackInt
a synonym of Stack' Int
. But in order to construct a value of this type you still need to use the constructor Stack'
(which has type [v] -> Int -> Stack' v
). So your code should be
data Stack' v = Stack' [v] Int deriving (Show)
main = print(Stack' [1,2,3] 4)
If you wanted to be sure that the type was Stack' Int
then you could add a function
data Stack' v = Stack' [v] Int deriving (Show)
stackInt :: [Int] -> Int -> Stack' Int
stackInt list i = Stack' list i
main = print(stackInt [1,2,3] 4)
EDIT: Not also that I've written stackInt list i = Stack' list i
for transparency here, but you can write it more elegantly just as stackInt = Stack'
. It is the type constraint that makes sure that you get the right type here.
You could also have both the new function and the type synonym if you wanted, ie
data Stack' v = Stack' [v] Int deriving (Show)
type StackInt = Stack' Int
stackInt :: [Int] -> Int -> StackInt
stackInt list i = Stack' list i
main = print(stackInt [1,2,3] 4)