Haskell数据类型错误

yon*_*tix 1 haskell types

我有这个功能:

data Memory = Memory
    {visited::[Point]
    ,dfsstack::[Point]
    ,currentPoz::Point
    }deriving(Eq)
perceiveAndAct :: SVal -> [Cardinal] -> a -> (Action, a)
perceiveAndAct s cs m
  | elem W cs == True && elem N cs == True && elem E cs == True && elem S cs == False = (Just S, Memory (visited m) (dfsstack m) (currentPoz m))
Run Code Online (Sandbox Code Playgroud)

把m而不是 Memory (visited m) (dfsstack m) (currentPoz m)工作正常,否则它给了我:

Couldn't match expected type `(a, b)'
           against inferred type `Memory -> Point'
    In the first argument of `fst', namely `currentPoz'
    In the first argument of `($)', namely `fst currentPoz'
    In the expression: fst currentPoz $ currentPoz m
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?

Dan*_*ner 5

你给的类型perceiveAndAct是非常多态的.相比:

id :: a -> a
id m = m -- the only correct implementation
id m = Memory (visited m) (dfsstack m) (currentPoz m) -- type error
-- only works for Memory, not all possible a

idMemory :: Memory -> Memory
id m = m -- this is fine
id m = Memory (visited m) (dfsstack m) (currentPoz m) -- also correct
Run Code Online (Sandbox Code Playgroud)

但是,我有点困惑,因为您粘贴的类型错误与我在您声明您所做的更改时获得的类型错误不符.也许你最好粘贴你使用的确切代码,它会产生错误以及你得到的确切错误,而不是正确的代码和一些我们看不到的隐形代码的错误.