Scala中的不可变列表 [英] Immutable Lists in Scala

查看:139
本文介绍了Scala中的不可变列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想弄清楚像List这样的不可变事物是如何工作的,以及如何向其中添加事物?

I am just trying to figure out how immutable things like a List are working, and how I can add things to it?

我很抱歉提出这样愚蠢的问题,但是为什么在打印时我的列表总是空白?

I am very sorry for asking such dumb questions, but why is here my list always empty when printing it out?

var end = false
val list = List()
while (!end) {
  val input = scala.io.StdIn.readLine("input:")
  if (input == "stop" ) end = true
  else input :: list
}

println(list)

}

对于给您带来的不便和这个非常愚蠢的问题,我们深表歉意!

Sorry for my inconvenience and this rather stupid question!

推荐答案

我只是想弄清楚像List这样的不可变事物是如何工作的,以及如何向其中添加事物?

I am just trying to figure out how immutable things like a List are working, and how I can add things to it?

不能.毕竟,这就是不可变的意思.如果不是拉丁文,那么 immutable 的英语翻译是 unchangeable .现在应该清楚了,为什么您不能更改不能更改 的东西.

You can't. That's what immutable means, after all. If Latin is not your cup of tea, the English translation of immutable is unchangeable. It should be clear now, why you can't change something that is unchangeable.

我很抱歉提出这样愚蠢的问题,但是为什么在打印时我的列表总是空白?

I am very sorry for asking such dumb questions, but why is here my list always empty when printing it out?

您创建了一个空列表,并且您永远都不会对其进行更改(因为它始终无法更改 ).因此,它当然是空的.

You create an empty list, and you never change it (because it cannot be changed anyway). So, of course it is empty.

您可以 做什么,那就是创建一个 new 列表,该列表与 old 列表几乎完全一样,除了新列表之外.项放在前面.那就是您在这里所做的:

What can you can do, however, is create a new list which is almost exactly like the old list, except with a new item prepended to the front. That's what you are doing here:

input :: list

但是,您不会在任何地方分配此新列表,也不会返回它,而完全忽略了它.

However, you don't assign this new list anywhere, you don't return it, you completely ignore it.

如果您想以任何方式实际使用列表,则需要以某种方式记住它.最明显的解决方案是将其分配给变量:

If you want to actually use your list in any way, you need to remember it somehow. The most obvious solution would be to assign it to a variable:

var end = false
var list: List[String] = List() // note: `var` instead of `val`
while (!end) {
  val input = scala.io.StdIn.readLine("input:")
  if (input == "stop" ) end = true
  else list = input :: list // note: assign to `list`
}
println(list)

但是,这不是很惯用.毕竟,我们现在已经获取了一个不可变列表,并将其分配给了一个 mutable 变量……IOW,我们刚刚将可变性移到了周围.

However, that's not very idiomatic. After all, we have now taken an immutable list and assigned it to a mutable variable … IOW, we have just moved the mutability around.

相反,我们可以使用递归解决方案:

Instead, we could use a recursive solution:

def buildListFromInput(list: List[String] = List()): List[String] = {
  val input = scala.io.StdIn.readLine("input:")
  if (input == "stop") list else buildListFromInput(input :: list)
}

println(buildListFromInput())

此解决方案不仅是递归的,递归调用也位于 tail位置(IOW,方法为 tail-recursive ),这意味着它将只是与while循环一样有效(实际上,它将被编译为while循环,或更准确地说,将其编译为GOTO). Scala语言规范保证了Scala的所有实现必须消除直接的尾部递归.

This solution is not only recursive, the recursive call is also in tail position (IOW, the method is tail-recursive), which means that it will be just as efficient as a while loop (in fact, it will be compiled into a while loop, or more precisely, into a GOTO). The Scala Language Specification guarantees that all implementations of Scala must eliminate direct tail-recursion.

这篇关于Scala中的不可变列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆