Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list exercise error #34

Open
mii9000 opened this issue Feb 2, 2018 · 5 comments · May be fixed by #59
Open

list exercise error #34

mii9000 opened this issue Feb 2, 2018 · 5 comments · May be fixed by #59

Comments

@mii9000
Copy link

mii9000 commented Feb 2, 2018

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?

@julienrf
Copy link
Collaborator

julienrf commented Feb 2, 2018

Hi, it’s true that the example doesn’t compile. The idea was more to show various kinds of patterns.

@mii9000
Copy link
Author

mii9000 commented Feb 2, 2018

For someone new to Scala like me this could be a bit confusing. I think this can be refactored to show two lists or remove the last case altogether.

@FRosner
Copy link
Member

FRosner commented Feb 2, 2018

I agree with @ibrahim-islam that any code should be copy-paste ready to try out and play with it. Maybe you want to make the code compile, @ibrahim-islam?

@mii9000
Copy link
Author

mii9000 commented Feb 2, 2018

Yes and while at it maybe do a PR to make this right for future learners :)
But would like your opinion on how it should be corrected.

@FRosner
Copy link
Member

FRosner commented Feb 5, 2018

@ibrahim-islam make a proposal :) Ping me if you like to discuss it :)

erikwright added a commit to erikwright/exercises-scalatutorial that referenced this issue May 2, 2019
While the example is interesting in that it shows an example of advanced pattern matching, it is problematic in that the pattern used is not of a valid type for the rest of the code.

As it is not really relevant to the issue at hand, and given that it's a relatively advanced example not likely to be relevant to someone first learning the language, I suggest simply removing it. A separate section on advanced pattern matching could potentially cover it.

Fixes scala-exercises#34
@erikwright erikwright linked a pull request May 2, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants