Functional programming Idris函数获取类型不匹配错误
我是伊德里斯的新手。我正在处理以下函数Functional programming Idris函数获取类型不匹配错误,functional-programming,idris,Functional Programming,Idris,我是伊德里斯的新手。我正在处理以下函数 average : String -> Double average str = let numWords = wordCount str totalLength = sum (allLengths (words str)) in cast totalLength / cast numWords where wordCount : String ->
average : String -> Double
average str
= let numWords = wordCount str
totalLength = sum (allLengths (words str)) in
cast totalLength / cast numWords
where
wordCount : String -> Nat
wordCount str = length (words str)
allLengths : List String -> List Nat
allLengths strs = map length strs
我一直得到以下错误
Type checking ./average.idr
average.idr:5:47:
|
5 | totalLength = sum (allLengths (words str)) in
| ^
When checking right hand side of average with expected type
Double
When checking argument x to type constructor =:
Type mismatch between
Nat (Type of Main.average, wordCount str _)
and
_ -> _ (Is Main.average, wordCount str _ applied to too many arguments?)
Holes: Main.average
我知道我已经声明average返回一个Double,但是我为average编写的声明没有返回Double。这就是我被难倒的地方。我希望演员能做这项工作。你的缩进被取消了。在未来,他们说 在编写Idris程序时,定义的顺序和缩进都很重要。。。新声明必须以与前一声明相同的缩进级别开始。或者,使用分号
代码>可用于终止声明
试试这个
average : String -> Double
average str
= let numWords = wordCount str
totalLength = sum (allLengths (words str)) in
cast totalLength / cast numWords
我猜在你的代码中,它将average
中wordCount str
之后的所有内容解析为wordCount str
的参数,这会导致类型错误,因为Nat
不接受参数。谢谢你。缩进并没有解决我的问题,但分号解决了,这是我已经用过的to@Oneiros真奇怪。我得到了和你一样的错误,但是当我改变缩进时,它编译得很好。这也是Idris书中的内容,但很高兴逗号分号起了作用