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 值声明中的Scala列表模式匹配_List_Scala_Pattern Matching_Tuples - Fatal编程技术网

List 值声明中的Scala列表模式匹配

List 值声明中的Scala列表模式匹配,list,scala,pattern-matching,tuples,List,Scala,Pattern Matching,Tuples,假设以下四个元组的列表: > def getList = List( (1,2), (3,4), (5,6), (7,8) ) 我想在新的值声明中将其解析为三个部分,如下-最后一个元组(l1,l2),倒数第二个元组(p1,p2)和列表的其余部分rest 此外,我还希望模式匹配元组。例如: > val (rest :: (p1, p2) :: (l1, l2) ) = getList rest = List( (1,2), (3,4) ) p1 = 5 p2 = 6 l1 = 7 l

假设以下四个元组的列表:

> def getList = List( (1,2), (3,4), (5,6), (7,8) )
我想在新的值声明中将其解析为三个部分,如下-最后一个元组
(l1,l2)
倒数第二个元组
(p1,p2)
列表的其余部分
rest

此外,我还希望模式匹配元组。例如:

> val (rest :: (p1, p2) :: (l1, l2) ) = getList
rest = List( (1,2), (3,4) )
p1 = 5
p2 = 6
l1 = 7
l2 = 8
预期的输出不起作用,因为
需要一个列表作为其右侧参数。我尝试了几种不同的可能性,例如使用
列表
包装器。然而,我还没有达到目标

有可能吗?我不想颠倒这个列表——相反,我正在寻找一个适用于值声明的优雅解决方案(因此,请不要
match
please)。我希望在一行解决方案


编辑:Scala代码运行程序版本2.9.2

您可以使用
:+
匹配器对象:

val rest :+ ((p1, p2)) :+ ((l1, l2))  = getList
从scala 2.10开始


对于以前的版本,您可以使用

非真实:-/
错误:未找到:值:+
错误:递归值x$1需要键入
@petrbel什么是scala库的版本?@petrbel它在2.10中提供