haskell中的通用堆栈实现

时间:2018-03-25 12:32:51

标签: haskell stack

我正在尝试创建堆栈操作的通用实现。编译时会出错:

ArrayList

这是我的代码:

List<User> tempList = users.stream()
                           .filter(e -> e.getFirstName().equals(firstName))
                           .collect(Collectors.toCollection(ArrayList::new));

1 个答案:

答案 0 :(得分:0)

以下是您的代码,其中包含三个更正:

-- do not derive Show since you define it
data Stack a = Stack [a]
               deriving (Eq,Ord)

-- there was a redundant pattern matching here
printelems :: (Show a) => [a] -> String
printelems []     = ""
printelems (x:xs) = if null xs then show x else show x ++ "->" ++ printelems xs

instance (Show a) => Show (Stack a)
 where
 show (Stack l) = printelems l

-- "Stack" was missing
empty :: Stack a
empty = Stack []

-- "Stack" was missing
push :: a -> Stack a -> Stack a
push x (Stack s) = Stack (x:s)