Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 模式上的分析错误_Haskell - Fatal编程技术网

Haskell 模式上的分析错误

Haskell 模式上的分析错误,haskell,Haskell,尝试实现一个函数,该函数将返回一个整数列表,表示每个双精度列表的顺序,例如: orderings [ [1.0, 2.0, 3.0], [3.0, 2.0, 1.0] ] > [ [0, 1, 2], [2, 1, 0] ] 但是,由于某些原因,我的模式匹配出现问题: import Data.List -- Return a list of orderings for each list of doubles orderings:: [[Double]] -> [Int] ord

尝试实现一个函数,该函数将返回一个整数列表,表示每个双精度列表的顺序,例如:

orderings [ [1.0, 2.0, 3.0], [3.0, 2.0, 1.0] ]
> [ [0, 1, 2], [2, 1, 0] ]
但是,由于某些原因,我的模式匹配出现问题:

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

我一辈子都看不到错误

必须在x:xs模式周围加括号:


必须在x:xs模式周围加上括号:

有3个错误:

在orderings x:xs=ordering x:orderings xs行中,您尝试cons:a list和list,但是cons生成了带有给定值的list,并且您忘记了x:xs cons模式匹配周围的参数

类型:为:

该行的正确形式为:

orderings (x:xs) = (ordering x) ++ (orderings xs)
由于++连接到列表:

Prelude> :type (++)
(++) :: [a] -> [a] -> [a]
最后一个错误是,在最后一行中,它应该是xs而不是x。有3个错误:

在orderings x:xs=ordering x:orderings xs行中,您尝试cons:a list和list,但是cons生成了带有给定值的list,并且您忘记了x:xs cons模式匹配周围的参数

类型:为:

该行的正确形式为:

orderings (x:xs) = (ordering x) ++ (orderings xs)
由于++连接到列表:

Prelude> :type (++)
(++) :: [a] -> [a] -> [a]

最后一个错误是,在最后一行中,它应该是xs而不是x

除了x:xs周围缺少括号外,还有两个问题:

订单类型错误;我想应该是[[Double]]->[[Int]]

x不在订购范围内;我想应该是xs

以下是更正后的代码:

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!

除了x:xs周围缺少括号外,还有两个问题:

订单类型错误;我想应该是[[Double]]->[[Int]]

x不在订购范围内;我想应该是xs

以下是更正后的代码:

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!
如前所述,排序的结果应该是[[Int]]。但是,功能的实现可以简化为:

import Data.List

-- Return a list of orderings for each list of doubles
orderings :: [[Double]] -> [[Int]]
orderings = map ordering

ordering :: [Double] -> [Int]
ordering = map snd . sort . flip zip [0..]
如前所述,排序的结果应该是[[Int]]。但是,功能的实现可以简化为:

import Data.List

-- Return a list of orderings for each list of doubles
orderings :: [[Double]] -> [[Int]]
orderings = map ordering

ordering :: [Double] -> [Int]
ordering = map snd . sort . flip zip [0..]

请参阅更新,它的模式为orderingsorderings=地图订购谢谢!此外,排序=映射fst。糟糕的是,比较一下。zip[0..]。可以在Data.Function中找到on;对于这些情况非常有用。请参阅更新,它的模式为orderingsorderings=map ordering谢谢!此外,排序=映射fst。糟糕的是,比较一下。zip[0..]。可以在Data.Function中找到on;对于这些情况非常有用。我记得我在10次中有9次出现此错误,当时以haskell开头的调用的返回类型中的预期类型Int“与实际类型[Int]”无法匹配:“,的第一个参数中的ordering”,即表达式:ordering x:orderings xsI中的'ordering x',我记得我在10次中有9次出现此错误,当时以Haskell开头的调用的返回类型中的'ordering'的预期类型Int'与实际类型[Int]'无法匹配:'的第一个参数中的'ordering',即:ordering x:orderings xsSorry-x中的'ordering x'是一个打字错误-我得到了xs-谢谢!但是你的签名是对的。对不起-x是个打字错误-我得到了x-谢谢!你的签名是对的。酷,太棒了!但是你怎么知道排序将使用第一项进行排序呢?@drozzy:元组默认是按字典顺序排序的,所以只要每个元组中的第一项与此处的不同,使用默认顺序与按第一项排序相同。@drozzy:哈马尔说的。这就是为什么我翻转了拉链,这样双打就成为元组中的第一个。酷,太棒了!但是你怎么知道排序将使用第一项进行排序呢?@drozzy:元组默认是按字典顺序排序的,所以只要每个元组中的第一项与此处的不同,使用默认顺序与按第一项排序相同。@drozzy:哈马尔说的。这就是我翻转拉链的原因,这样双倍就可以成为元组中的第一个。