Skip to content

list exercise error #34

Open
Open
@mii9000

Description

@mii9000

According to https://github.com/scala-exercises/exercises-scalatutorial/blob/master/src/main/scala/scalatutorial/sections/StandardLibrary.scala#L72-L97

val nums = Nil.::(4).::(3).::(2).::(1)

nums match {
  // Lists of `Int` that starts with `1` and then `2`
  case 1 :: 2 :: xs => …
  // Lists of length 1
  case x :: Nil => …
  // Same as `x :: Nil`
  case List(x) => …
  // The empty list, same as `Nil`
  case List() =>
  // A list that contains as only element another list that starts with `2`
  case List(2 :: xs) => …
}

the last case of case List(2 :: xs) gives error because the inferred type is List[Int] but the case signature is expecting List[List[Int]]. Please correct me if I am wrong. If I am not then how should it be re-written?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions