【发布时间】:2014-07-30 19:11:21
【问题描述】:
这是我第一次使用 Haskell,我已经阅读了很多关于它的教程。但是到了实践的时候,就会出现很多问题。 我正在尝试制作一个堆栈数据结构并在 Do 块中使用它。但是当我这样做时。它说不能将类型'Stack'与'IO'匹配,我不知道这个问题。以下是我的代码:
import Data.Array.IO
main::IO()
main = do
arr <- newArray((0,0),(8,13)) 0 ::IO (IOArray(Int,Int) Int)
list <- getElems arr
print list
push 0 mystack --here is the problem
return()
data Stack a = Stack [a] deriving Show
empty :: Stack a
empty = Stack []
push :: a -> Stack a -> Stack a
push x (Stack xs)= Stack (x:xs)
pop :: Stack a -> (Maybe a, Stack a)
pop (Stack []) = (Nothing, Stack [])
pop (Stack (x:xs)) = (Just x, Stack xs)
mystack = empty
问题如下(当我将 push 0 mystack 放在 Do 块中时,它会显示出来)
Couldn't match type `Stack' with `IO'
Expected type: IO Integer
Actual type: Stack Integer
In the return type of a call of `push'
In a stmt of a 'do' block: push 0 mystack
【问题讨论】: