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

把 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

可能是什么问题呢?

4

2 回答 2

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

但是,我有点困惑,因为您粘贴的类型错误与我在进行您声称所做的更改时得到的类型错误不匹配。也许您最好将您使用的给出错误的确切代码与您得到的确切错误一起粘贴,而不是正确的代码和一些我们看不到的不可见代码的错误。

于 2012-04-14T18:49:16.680 回答
-1

visited, dfsstack, 和currentPoz是函数,它们不构造列表。

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

visited, dfsstack, 和是给定currentPoz的函数,可以提取这些元素中的每一个。 someData :: Memory

您还需要将perceiveAndAct参数“m”的类型从:: a更改为:: Point

于 2012-04-14T18:44:56.867 回答