Scala链表

Scala链表,scala,Scala,在Scala中迈出第一步时,我遇到了第一个误解。 我举了一个链表的经典例子 sealed trait List[+A] // `List` data type, parameterized on a type, `A` case object Nil extends List[Nothing] // A `List` data constructor representing the empty list /* Another data constructor, representing non

在Scala中迈出第一步时,我遇到了第一个误解。
我举了一个链表的经典例子

sealed trait List[+A] // `List` data type, parameterized on a type, `A`
case object Nil extends List[Nothing] // A `List` data constructor representing the empty list
/* Another data constructor, representing nonempty lists. Note that `tail` is another `List[A]`,
which may be `Nil` or another `Cons`.
 */
case class Cons[+A](head: A, tail: List[A]) extends List[A]

object List { // `List` companion object. Contains functions for creating and working with lists.
  def sum(ints: List[Int]): Int = ints match { // A function that uses pattern matching to add up a list of integers
    case Nil => 0 // The sum of the empty list is 0.
    case Cons(x,xs) => x + sum(xs) // The sum of a list starting with `x` is `x` plus the sum of the rest of the list.
  }

  def product(ds: List[Double]): Double = ds match {
    case Nil => 1.0
    case Cons(0.0, _) => 0.0
    case Cons(x,xs) => x * product(xs)
  }

  def apply[A](as: A*): List[A] = // Variadic function syntax
    if (as.isEmpty) Nil
    else Cons(as.head, apply(as.tail: _*))

  val x = List(1,2,3,4,5) match {
    case Cons(x, Cons(2, Cons(4, _))) => x
    case Nil => 42
    case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
    case Cons(h, t) => h + sum(t)
    case _ => 101
  }
}
有几个问题:
1) 为什么自定义链表类与内置的
scala.collection.immutable.List.type不冲突

2) 当我们将内置列表与自定义链表匹配时,为什么一段代码应该是正确的

  val x = List(1,2,3,4,5) match {
    case Cons(x, Cons(2, Cons(4, _))) => x
    case Nil => 42
    case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
    case Cons(h, t) => h + sum(t)
    case _ => 101
  }
  • 自定义链表类与内置的
    scala.collection.immutable.list.type
    不冲突,因为本地声明(如自定义列表类型)的优先级高于导入(即使是非显式声明,如scala内置的
    list
    )。有关完整的优先顺序,请参见的第2章

  • 引用的匹配代码与内置列表不匹配,而是与您自己的本地声明列表匹配。通过将列表重命名为类似CustomList的名称,您可以自己查看它,并看到将出现一些错误,或者将内置列表完全限定为以下代码

  • 以下代码实际上将内置列表与自定义列表结构相匹配,并且不会编译:

      val x = scala.collection.immutable.List(1,2,3,4,5) match {
        case Cons(x, Cons(2, Cons(4, _))) => x
        case Nil => 42
        case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
        case Cons(h, t) => h + sum(t)
        case _ => 101
      }
    
  • 自定义链表类与内置的
    scala.collection.immutable.list.type
    不冲突,因为本地声明(如自定义列表类型)的优先级高于导入(即使是非显式声明,如scala内置的
    list
    )。有关完整的优先顺序,请参见的第2章

  • 引用的匹配代码与内置列表不匹配,而是与您自己的本地声明列表匹配。通过将列表重命名为类似CustomList的名称,您可以自己查看它,并看到将出现一些错误,或者将内置列表完全限定为以下代码

  • 以下代码实际上将内置列表与自定义列表结构相匹配,并且不会编译:

      val x = scala.collection.immutable.List(1,2,3,4,5) match {
        case Cons(x, Cons(2, Cons(4, _))) => x
        case Nil => 42
        case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
        case Cons(h, t) => h + sum(t)
        case _ => 101
      }
    

    我相信你的问题是关于范围的。您已经定义了自己的列表,该列表与scala.collection.immutable中的列表无关。。。Cons和Nil也一样

    当您在第2部分中实例化列表时,您正在实例化您的列表,而不是Scala库中的列表


    还是我遗漏了什么?

    我相信,你的问题其实是关于范围的。您已经定义了自己的列表,该列表与scala.collection.immutable中的列表无关。。。Cons和Nil也一样

    当您在第2部分中实例化列表时,您正在实例化您的列表,而不是Scala库中的列表


    还是我遗漏了什么?

    谢谢你的回复。但是如何使用构造列表(1、2、3、4、5)?应用方法是否提供了这样的机会?您正在调用
    List
    对象的
    apply[A](as:A*):List[A]
    方法。感谢您的回复。但是如何使用构造列表(1、2、3、4、5)?应用方法是否提供了这样的机会?您正在调用
    List
    对象的
    apply[A](as:A*):List[A]
    方法。