简体   繁体   English

Haskell Data.Sequence试图同时更改2个值

[英]Haskell, Data.Sequence trying to change 2 values at same time

Let's say I have a = [[1],[2,3,4],[5,6,7]] and I would like to change 2 values in this list, let's call every list in a "level" and it starts from "0". 假设我有a = [[1],[2,3,4],[5,6,7]] ,我想更改此列表中的2个值,让我们将每个列表a “级别”,从“ 0”开始。 I know that the 2 values are in neighbouring levels and I know their positions. 我知道这两个值在相邻级别中,并且知道它们的位置。 Let's say I want to change the numbers "4" and "5" both with "9". 假设我想将数字“ 4”和“ 5”都更改为“ 9”。 Here is my code : 这是我的代码:

import qualified Data.Sequence as Seq

updateList :: Seq.Seq a -> Int -> a -> a -> Int -> Int -> Seq.Seq a
updateList list levelAt firstNew secondNew pos1 pos2
 | 0 == (levelAt - 1) = (Seq.update pos1 firstNew (Seq.take 1 list)) Seq.>< (updateList (Seq.drop 1 list) (levelAt - 1 ) firstNew secondNew pos1 pos2 )
 | 0 == levelAt = (Seq.update pos2 secondNew (Seq.take 1 list)) Seq.>< (Seq.drop 1 list)
 | otherwise = (Seq.take 1 list) Seq.>< (updateList (Seq.drop 1 list) (levelAt - 1) firstNew secondNew pos1 pos2 )

For my example I have to call this function with : 对于我的示例,我必须使用以下命令调用此函数:

updateList (Seq.fromList [[1],[2,3,4],[5,6,7]]) 2 9 9 2 0
Btw when I call this function it would always be with [[a]] type. 顺便说一句,当我调用此函数时,它将始终为[[a]]类型。

But It gives me an error No instance for (Num t0) arising from the literal '1' The type variable 't0' is ambiguous 但这给我一个错误No instance for (Num t0) arising from the literal '1' The type variable 't0' is ambiguous
Can you explain me why this is happening and how to fix it ? 您能解释一下为什么会发生这种情况以及如何解决吗?
Thank you. 谢谢。

Your Seq.update functions operate on sequences, not on the sequences within your sequence, because Seq.update :: Int -> a -> Seq.Seq a -> Seq.Seq a ; 您的Seq.update函数对序列起作用,而不对序列中的序列Seq.update :: Int -> a -> Seq.Seq a -> Seq.Seq a ,因为Seq.update :: Int -> a -> Seq.Seq a -> Seq.Seq a ; it doesn't even know you have sequences inside the sequence. 它甚至都不知道序列中有序列。

The type error you got results from it trying to make a list of integers out of 1 , so complaining that it didn't have a Num instance for list. 您尝试从1中获取整数列表的结果是类型错误,因此抱怨它没有用于列表的Num实例。

ghci> updateList (Seq.fromList [[1],[2,3,4],[5,6,7]]) 0 [9] [9] 1 2
fromList [[1],[2,3,4],[5,6,7]]

Unfortunately this means your function isn't doing what you intended. 不幸的是,这意味着您的功能没有达到您的预期。

EDIT 编辑

As a commenter pointed out, the type of your sequence is [a] . 正如评论者所指出的,序列的类型是[a] Thus the third and fourth arguments of updateList need to have the type [a] , but they have type a . 因此, updateList的第三个和第四个参数必须具有类型[a] ,但是它们具有类型a

From the signature of updateList , you can tell that updateList is polymorphic because of the letter a . updateList的签名可以看出,由于字母aupdateList是多态a When you call updateList , the types of the polymorphic arguments must be inferred from the context, or given explicitly. 当您调用updateList ,必须从上下文中推断或显式地指定多态参数的类型。 In your case, the type of the sequence that you pass in is unclear. 在您的情况下,您不清楚传递的序列类型。 Is it a sequence of Int s, Integer s, or something else? 它是IntInteger或其他东西的序列吗? You can fix this problem by explicitly giving the type of the sequence: 您可以通过明确给出序列的类型来解决此问题:

updateList (Seq.fromList [[1 :: Int],[2,3,4],[5,6,7]]) 2 [9] [9] 3 1

or 要么

updateList (Seq.fromList [[1],[2,3,4],[5,6,7]] :: Seq.Seq [Int]) 2 [9] [9] 3 1

Alternatively, since the polymorphic type a appears in several places in the signature, you only need to specify the type of one argument. 另外,由于多态类型a出现在签名中的多个位置,因此您只需要指定一个参数的类型即可。 Thus you could give an explicit type to either of the 9s: 因此,您可以为这9个数字中的任意一个提供明确的类型:

updateList (Seq.fromList [[1],[2,3,4],[5,6,7]]) 2 [9::Int] [9] 3 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM