1

我正在学习 Purescript By Example 教程,但我无法使用左侧折叠来排列类型:

smallestFile' :: [Path] -> Maybe Path
smallestFile' (x : xs) = foldl(\acc i -> smallerFile(acc i) ) Just(x) xs // Error is on this line

smallerFile :: Maybe Path -> Path -> Maybe Path
smallerFile maybeA b = do
  a  <- maybeA
  sa <- size a
  sb <- size b
  if sa > sb then return(b) else return(a)

我收到的错误在左边,是

Cannot unify Prim.Function u13116 with Data.Maybe.Maybe

我相信这些类型是一致的,但我不能对这个错误做出正面或反面。

此外,是否可以清理匿名函数语法,以便

foldl(\acc i -> smallerFile(acc i) ) Just(x) xs

变成这样:

foldl smallerFile Just(x) xs
4

1 回答 1

4

在 PureScript 中,与 Haskell 一样,函数应用程序使用空格,并关联到左侧,这意味着f x y z解析为((f x) y) z. 只有在需要重新组合术语时才需要括号。看起来您正在尝试将括号用于函数应用程序。

我怀疑你想写的是

foldl (\acc i -> smallerFile acc i) (Just x) xs

to 的参数foldl是一个函数,它接受两个参数acci返回应用程序smallerFile acc i。这相当于双重申请(smallerFile acc) i。首先我们应用参数acc,然后应用第二个参数i。解析器中函数应用的优先规则使这些等价。

另外,Just x需要用括号括起来,因为您编写的内容解析为

foldl (\acc i -> smallerFile (acc i)) Just x xs

这为foldl.

一旦你有了正确的版本,你会注意到它\acc i -> smallerFile acc i等同于\acc -> (\i -> (smallerFile acc) i). 内部函数i立即应用其参数,因此我们可以将其简化为\acc -> smallerFile acc. 再次应用这个简化,我们得到smallerFile,所以代码变成:

foldl smallerFile (Just x) xs

所以最后唯一的错误是Just x.

于 2015-02-23T20:08:50.293 回答