2012-04-14 67 views
1

我有这样的功能:哈斯克尔的数据类型错误

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)) 

把米,而不是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 

可能是什么问题呢?

+2

错误信息所指的'fst currentPoz $ currentPoz m'在哪里? – hammar 2012-04-14 18:52:16

+1

顺便说一句,无论你有什么'blah == True',你都可以简化为'blah'(例如,简化'elem W cs == True'到'elem W cs')。同样,您可以将'elem S cs == False'简化为'notElem S cs'。 – dave4420 2012-04-14 20:26:18

回答

-1

visiteddfsstackcurrentPoz是函数,它们不构造列表。

你想写Memory [m] [m] m,而不是。

visiteddfsstack,和currentPoz是哪个,给出someData :: Memory,可以提取每个元件的功能。

你还需要的perceiveAndAct的说法从0​​‘m’的类型更改为:: Point

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 

不过,我有点困惑,因为你粘贴的类型错误时,我让你声称你所做的更改不匹配类型的错误,我得到。也许你最好将你使用的确切代码粘贴到你得到的确切错误上,而不是正确的代码和我们无法看到的一些隐形代码的错误。