Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/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
List 如何在Haskell中返回元组列表 我在C++中进行了一个优化课程,一直在尝试在哈斯克尔的实验室里复习踢和咯咯的笑。事情很艰难,但很有趣_List_Haskell - Fatal编程技术网

List 如何在Haskell中返回元组列表 我在C++中进行了一个优化课程,一直在尝试在哈斯克尔的实验室里复习踢和咯咯的笑。事情很艰难,但很有趣

List 如何在Haskell中返回元组列表 我在C++中进行了一个优化课程,一直在尝试在哈斯克尔的实验室里复习踢和咯咯的笑。事情很艰难,但很有趣,list,haskell,List,Haskell,我正在尝试编写一个函数,返回一个包含3个整数元组的列表,如下所示: [(1,1,1)、(1,2,1)、(1,3,2)] 以下是我一直尝试使用的代码: sortToTuples :: (Num a) => [a] -> Int -> Int -> [(Int,Int,a)] sortToTuples [] _ _ = [] -- i and j are passed as 1 and 1. sortToTuples (x:xs) i j | j > 9

我正在尝试编写一个函数,返回一个包含3个整数元组的列表,如下所示:

[(1,1,1)、(1,2,1)、(1,3,2)]

以下是我一直尝试使用的代码:

sortToTuples :: (Num a) => [a] -> Int -> Int -> [(Int,Int,a)]
sortToTuples [] _ _ = []
-- i and j are passed as 1 and 1.
sortToTuples (x:xs) i j   
    | j > 9         = [(i+1, 1, x)] ++ sortToTuples (xs i+1 1)
    | otherwise     = [(i, j+1, x)] ++ sortToTuples (xs i, j+1)
该函数用于获取表示数独谜题的平面列表,并返回元组列表(i,j,x),其中i是行值,j是列值,x是单元格值

无论出于何种原因,haskell对我的类型签名非常不满意:

Prelude> :l quicksort.hs 
[1 of 1] Compiling Main             ( quicksort.hs, interpreted )

quicksort.hs:23:44:
    Couldn't match expected type `[(Int, Int, a)]'
                with actual type `Int -> Int -> [(Int, Int, a0)]'
    In the return type of a call of `sortToTuples'
    Probable cause: `sortToTuples' is applied to too few arguments
    In the second argument of `(++)', namely
      `sortToTuples (xs i + 1 1)'
    In the expression: [(i + 1, 1, x)] ++ sortToTuples (xs i + 1 1)
Failed, modules loaded: none.
Prelude> 

这里有一点语法错误

| j > 9 = [(i+1, 1, x)] ++ sortToTuples (xs i+1 1)
| otherwise = [(i, j+1, x)] ++ sortToTuples (xs i, j+1)
你几乎说对了,应该是这样的

... sortToTuples xs (i+1) 1
... sortToTuples xs i (j+1)
这样,每个参数将分别传递给
sortTuples


为了解释编译器错误,它将
(xsi+1)
视为一个单独的参数,并且由于它解析的Haskell是正确的,它认为
sortToTuples
的第一个参数应该具有类型
[a]
,因此它认为
sorttuples(xsi+1)
应该具有类型
Int->[Int,Int,a]

这里有一点语法错误

| j > 9 = [(i+1, 1, x)] ++ sortToTuples (xs i+1 1)
| otherwise = [(i, j+1, x)] ++ sortToTuples (xs i, j+1)
你几乎说对了,应该是这样的

... sortToTuples xs (i+1) 1
... sortToTuples xs i (j+1)
这样,每个参数将分别传递给
sortTuples


为了解释编译器错误,它将
(xsi+1)
视为一个单独的参数,并且由于它解析的Haskell是正确的,它认为
sortToTuples
的第一个参数应该具有类型
[a]
,因此它认为
sorttuples(xsi+1)
应该具有类型
Int->[Int,Int,a]

尝试
sortTuples xs(i+1)1
而不是
sortTuples(xs i+1)
尝试
sortTuples xs(i+1)1
而不是
sortTuples(xs i+1)
@SteelNation没问题,有时候你只需要另一双眼睛看着你的代码就能发现简单的错误。@SteelNation没问题,有时,您只需要另一组眼睛来查看代码,以捕获简单的错误。