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 为什么这个列表是空的_List_Scala_Match - Fatal编程技术网

List 为什么这个列表是空的

List 为什么这个列表是空的,list,scala,match,List,Scala,Match,我想在scala中创建一个函数,它使用“match…case”将列表的值加倍 例如: doubleList(List(2,1,4,5)) //> res0: List[Int] = List(4, 2, 8, 10) 我写了这个函数: def doubleList(xs: List[Int]): List[Int] = xs match { case y :: ys => y * 2; doubleList(ys); case Nil

我想在scala中创建一个函数,它使用“match…case”将列表的值加倍

例如:

doubleList(List(2,1,4,5))
//> res0: List[Int] = List(4, 2, 8, 10)
我写了这个函数:

def doubleList(xs: List[Int]): List[Int] =
    xs match {
      case y :: ys =>
        y * 2; doubleList(ys);
      case Nil => xs;
    }
但结果我得到了一个空列表:

//> res0: List[Int] = List()

有人能告诉我我做错了什么吗?

代码关闭语句并有效地丢弃结果,使用
创建一个新的
列表
,结果为
y*2
doubleList(ys)


另外,你不必输入
在scala中的一行末尾。

关闭语句并有效地丢弃结果,使用
创建一个新的
列表
,结果为
y*2
doubleList(ys)


另外,你不必输入
在scala中的一行末尾。

任何原因,为什么你不直接使用
map(*2)
?是的,我被要求具体使用“匹配…案例”。任何原因,为什么你不直接使用
map(*2)
?是的,我被要求具体使用“匹配…案例”。哦,我不知道这一点;结束语句。谢谢你的回答,效果很好。哦,我不知道;结束语句。谢谢你的回答,效果很好。
def doubleList(xs: List[Int]): List[Int] =
  xs match {
    case y :: ys =>
      y * 2 :: doubleList(ys)
    case Nil => xs
  }