解析模式错误 [英] Parse error on pattern
本文介绍了解析模式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
订单[[1.0,2.0,3.0],[3.0,2.0,1.0]]
> [[0,1,2],[2,1,0]]
与我的模式匹配出于某种原因:
import Data.List
- 返回列表每个双打名单的排序
orderings :: [[Double]] - > [Int]
orderings [] = []
排序x:xs =(排序x):(排序xs)
排序:: [Double] - > [Int]
排序xs = [i | (i,_)< - sorted]其中
sorted = sortBy(\(i1,e1)(i2,e2) - > compare e1 e2)$ zip [0 ..] xs
错误是:
模式中的分析错误:排序
看不到我生命中的错误! / p>
解决方案
还有两个问题(除了 x:xs
):
-
orderings 类型是错误的;我怀疑它应该是[[Double]] - > [[Int]]
-
x
code>排序;我怀疑它应该是xs
:
import Data.List
- 返回每个双打列表的排序列表
排序:: [[Double]] - > [[Int]] - 更改类型
排序[] = []
排序(x:xs)=(排序x):排序xs
排序:: [Double] - > [Int]
排序xs = [i | (i,_)< - sorted]其中
sorted = sortBy(\(i1,e1)(i2,e2) - > compare e1 e2)$ zip [0 ..] xs - xs not X!
Trying to implement a function that will return a list of ints the represent an ordering of each list of doubles, e.g.:
orderings [ [1.0, 2.0, 3.0], [3.0, 2.0, 1.0] ]
> [ [0, 1, 2], [2, 1, 0] ]
However, having trouble with my pattern matching for some reason:
import Data.List
-- Return a list of orderings for each list of doubles
orderings:: [[Double]] -> [Int]
orderings [] = []
orderings x:xs = (ordering x):(orderings xs)
ordering:: [Double] -> [Int]
ordering xs = [i | (i, _) <- sorted] where
sorted = sortBy (\(i1, e1) (i2,e2) -> compare e1 e2) $ zip [0..] xs
Error is:
Parse error in pattern: orderings
Can't see the error for the life of me!
解决方案
Two more problems (in addition to the missing parentheses around x:xs
):
the type of
orderings
is wrong; I suspect it should be[[Double]] -> [[Int]]
x
is not in scope inordering
; I suspect it should bexs
Here's the corrected code:
import Data.List
-- Return a list of orderings for each list of doubles
orderings:: [[Double]] -> [[Int]] -- changed type
orderings [] = []
orderings (x:xs) = (ordering x):(orderings xs)
ordering:: [Double] -> [Int]
ordering xs = [i | (i, _) <- sorted] where
sorted = sortBy (\(i1, e1) (i2,e2) -> compare e1 e2) $ zip [0..] xs -- xs not x!
这篇关于解析模式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文