HASKELL PROGRAMMING LANGUAGE
Problem 4 [7 POINTS]: Define a module named “ListIntStack” to represent the STACK data structure. We implement the STACK with Int list. For example, [3,2,1] represents a STACK with three elements and the top element is 3.
The “ListStack” module is described as follows:
(1) [1 POINTS]: The module includes a data type named “Stack”, which only has one value constructor named “IntStack”. The “IntStack” constructor only has one field represented by an Int list.
(2) [5 POINTS]: The module has five functions: “empty”, “push”, “pop”, “top”, “isEmpty”, which are described as follows.
(2.1) The “empty” function creates an empty STACK (represented by an empty Int list). The function type is
empty :: Stack
(2.2) The “push” function pushes an element on the top of the input STACK. The function type is
push :: Int -> Stack -> Stack
(2.3) The “pop” function pops the top element off the input STACK. If the input STACK is empty, the “pop” function returns an empty stack. The function type is
pop :: Stack -> Stack
(2.4) The “top” function returns the top element of the input STACK. It returns “Nothing” if the input STACK is empty. The function type is
top :: Stack -> Maybe Int
(2.5) The “isEmpty” function checks whether the input STACK is empty. It returns “True” if and only if the input STACK is empty. The function type is
isEmpty :: Stack -> Bool
(3) [1 POINTS]: The module exports all five functions and does NOT export any value constructors of “Stack” data type.
The “pop” function pops the top element off the input STACK. If the input STACK is empty, the “pop” function returns an empty stack. The function type is
pop :: Stack -> Stack
The “top” function returns the top element of the input STACK. It returns “Nothing” if the input STACK is empty. The function type is