解释一些 Scala 代码 - 初学者 [英] Explain some scala code - beginner

查看:37
本文介绍了解释一些 Scala 代码 - 初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个 Scala 代码,我正在尝试弄清楚它在做什么,除了它返回一个 int 的事实.我不确定这三行:

I've encountered this scala code and I'm trying to work out what its doing except the fact it returns an int. I'm unsure of these three lines :

  l match {
  case h :: t =>
  case _ => 0

功能:

def iterate(l: List[Int]): Int = 
  l match {
  case h :: t =>
    if (h > n) 0 
  case _ => 0
} 

推荐答案

首先,您定义一个名为 iterate 的函数,并将返回类型指定为 Int.它有 arity 1,类型 List[Int] 的参数 l.

First, you define a function called iterate and you specified the return type as Int. It has arity 1, parameter l of type List[Int].

List 类型在整个函数式编程中都很突出,它的主要特点是它具有高效的前置和易于将任何 List 分解为头和尾.头部将是列表的第一个元素(如果非空),尾部将是 List(它本身是一个 List)的其余部分 - 这变成对操作 List 的递归函数很有用.

The List type is prominent throughout functional programming, and it's main characteristics being that it has efficient prepend and that it is easy to decompose any List into a head and tail. The head would be the first element of the list (if non-empty) and the tail would be the rest of the List(which itself is a List) - this becomes useful for recursive functions that operate on List.

match 被称为模式匹配.它本质上是 C 语言中的 switch 语句,但功能更强大 - switch 将您限制为常量(至少在 C 中是这样),但是 match 没有这样的限制.

The match is called pattern matching.. it's essentially a switch statement in the C-ish languages, but much more powerful - the switch restricts you to constants (at least in C it does), but there is no such restriction with match.

现在,你的第一个 case 你有 h :: t - :: 被称为缺点",另一个来自函数式的术语编程.当您通过前置从另一个 List 创建一个新的 List 时,您可以使用 :: 运算符来执行此操作.

Now, your first case you have h :: t - the :: is called a "cons", another term from functional programming. When you create a new List from another List via a prepend, you can use the :: operator to do it.

示例:

val oldList = List(1, 2, 3)
val newList = 0 :: oldList // newList == List(0, 1, 2, 3)

在 Scala 中,以 : 结尾的运算符实际上是右侧的方法,因此 0 :: oldList 相当于 oldList.::(0) - 0 :: oldList 是语法糖,使阅读更容易.

In Scala, operators that end with a : are really a method of the right hand side, so 0 :: oldList is the equivalent of oldList.::(0) - the 0 :: oldList is syntactic sugar that makes it easier to read.

我们可以像这样定义 oldList

val oldList = 1 :: 2 :: 3 :: Nil

其中 Nil 代表一个空的 List.将其分解为多个步骤:

where Nil represents an empty List. Breaking this down into steps:

  1. 3 :: Nil 首先被评估,创建一个 List(3) 的等价物,它有头 3 和空尾.
  2. 2 被添加到上面的列表中,创建一个带有头 2 和尾 List(3) 的新列表.
  3. 1 是前置的,创建一个带有头 1 和尾 List(2, 3) 的新列表.
  1. 3 :: Nil is evaluated first, creating the equivalent of a List(3) which has head 3 and empty tail.
  2. 2 is prepended to the above list, creative a new list with head 2 and tail List(3).
  3. 1 is prepended, creating a new list with head 1 and tail List(2, 3).

结果 List(1, 2, 3)List 被分配给 val oldList.

The resulting List of List(1, 2, 3) is assigned to the val oldList.

现在,当您使用 :: 进行模式匹配时,您实际上将 List 分解为头部和尾部,就像我们创建 List 以上.当你这样做时

Now when you use :: to pattern match you essentially decompose a List into a head and tail, like the reverse of how we created the List above. Here when you do

l match {
  case h :: t => ...
}

你是说如果可能的话,将 l 分解成头和尾.如果分解成功,则可以使用这些 ht 变量来做任何你想做的事情......通常你会做一些像对 h 并在 t 上调用递归函数.

you are saying decompose l into a head and tail if possible. If you decompose successfully, you can then use these h and t variables to do whatever you want.. typically you would do something like act on h and call the recursive function on t.

这里要注意的一件事是你的代码不会编译..你做了一个 if (h > n) 0 但没有明确的 else 那又怎样发生在编译器看来你的代码是这样的:

One thing to note here is that your code will not compile.. you do an if (h > n) 0 but there is no explicit else so what happens is your code looks like this to the compiler:

if (h > n) 0
else { }

具有类型 AnyVal(0 和nothing"的常见超类型),违反了您的 Int 保证 - 你是将不得不添加一个 else 分支,其中包含一些失败值或其他内容.

which has type AnyVal (the common supertype of 0 and "nothing"), a violation of your Int guarentee - you're going to have to add an else branch with some failure value or something.

第二个 case _ => 就像 switch 中的 default,它捕获任何在你的第一个 case.

The second case _ => is like a default in the switch, it catches anything that failed the head/tail decomposition in your first case.

您的代码基本上是这样做的:

Your code essentially does this:

  1. l List参数,看看能不能分解成head和tail.
  2. 如果可以,将头部与(我假设的)外部作用域中名为 n 的变量进行比较.如果它大于n,函数返回0.(如果不大于,你需要添加会发生什么)
  3. 如果不能分解,函数返回0.
  1. Take the l List parameter and see if it can be decomposed into a head and tail.
  2. If it can be, compare the head against (what I assume to be) a variable in the outer scope called n. If it is greater than n, the function returns 0. (You need to add what happens if it's not greater)
  3. If it cannot be decomposed, the function returns 0.

这篇关于解释一些 Scala 代码 - 初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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