scala - Scala 函数返回行为
问题描述
我正在尝试解决这个 epfl scala 课程作业,他们要求我写一个列表的总和。如果我使用 return 语句或 if 使用 match 语句,我可以做到这一点,但我不明白为什么在没有 return 语句的情况下使用 if/else 不起作用。
def sum(xs: List[Int]): Int = xs {
if (xs.isEmpty) {
0
} else {
xs.head + sum(xs.tail)
}
}
我运行 sum(List(1,3,2)) 时得到的运行时错误是
java.lang.IndexOutOfBoundsException: 0
at scala.collection.LinearSeqOptimized.apply(LinearSeqOptimized.scala:67)
at scala.collection.LinearSeqOptimized.apply$(LinearSeqOptimized.scala:65)
at scala.collection.immutable.List.apply(List.scala:89)
at example.Lists$.sum(Lists.scala:39)
at example.Lists$.sum(Lists.scala:39)
at example.Lists$.sum(Lists.scala:39)
at example.Lists$.sum(Lists.scala:39)
如果我将代码中的 0 替换为 100 ,则错误消息将更改为 java.lang.IndexOutOfBoundsException: 100 。就像它正在尝试访问列表的第 N 个元素,而我所需要的只是返回。如果我添加两个返回语句,则所有操作都按预期工作。
你能解释一下吗?
解决方案
根本原因在函数声明的第一行:
def sum(xs: List[Int]): Int = xs {
这相当于:
def sum(xs: List[Int]): Int = xs.apply(<Some function>)
例如xs.apply(3)
意味着从列表中获取索引为 3 的项目xs
。
Scala 将评估括号内的代码,并尝试将结果应用到xs
. 当它展开递归时,它最终会到达一个空列表,但程序要求它返回xs(0)
不存在的列表,因此出现IndexOutOfBoundsException
错误。
要让这个程序做你想做的事情,只需xs
从你的函数体的开头删除:
def sum(xs: List[Int]): Int = {
现在:
sum(List(1,3,2))
res0: Int = 6
推荐阅读
- javascript - 嵌入数据表组件时获取函数内部的行数据
- liquibase - Liquibase 没有按依赖顺序执行更改日志文件
- excel - 在两个工作簿之间复制和粘贴动态范围
- php - 联系表格未在数据库中注册?
- kubernetes - GCP Kubernetes 集群监控限制图
- postgresql - 如何在 postgresql 中编写函数以根据传递给它的月份名称获取月份的开始日期和结束日期
- elasticsearch - Elastic Search 和 Apache Ignite 之间的发布订阅
- javascript - elevateZoom - jQuery 图像缩放插件
- reactjs - 如何在反应钩子路由器中通过useRoutes传递道具?
- reinforcement-learning - 具有 Q 学习的简单神经网络