Some questions regarding scala warm up

yc81reja
Erstie
Erstie
Beiträge: 12
Registriert: 18. Apr 2015 16:25

Some questions regarding scala warm up

Beitrag von yc81reja »

Hello
For entire Saturday I finally finished assignment 1 but I still got some weird warnings, and to be honest, violate the constraints of only using :: operator.
So I post my implementation of flatten here:

Code: Alles auswählen

import Util._

object Solution {
  
  def flatten[A](xss : List[List[A]]) : List[A] = {
    def flat[A](ret: List[A], xss: List[_]) : List[A] = xss match {
      case Nil => ret
      case ((x: List[A]) :: (ss: List[A])) => flat(ret ::: x, ss)
    }
    flat(Nil, xss)
  }
}
It's somehow ugly but working well by casting predefined List[List[A]] to List[_] so that we won't get the compilation error of mismatched type in the deepest recursion, i.e. List[A]. But when I tried to match x and ss it pumps the warning:

Code: Alles auswählen

solution.scala:8: warning: non variable type-argument A in type pattern List[A] is unchecked since it is eliminated by erasure
      case ((x: List[A]) :: (ss: List[A])) => flat(ret ::: x, ss)
                ^
solution.scala:8: warning: non variable type-argument A in type pattern List[A] is unchecked since it is eliminated by erasure
      case ((x: List[A]) :: (ss: List[A])) => flat(ret ::: x, ss)
It indicates that the List[A] that defines the type of x and ss is not taken into consideration. But if I delete them x and ss are implicitly deduced as Any, which will cause type mismatch error. Is there any way to suppress this warning?
In addition I used ::: to append list which is forbidden, so how can I append list with only :: ?
Thanks beforehand

nadolskyy
Windoof-User
Windoof-User
Beiträge: 24
Registriert: 9. Jul 2010 01:30

Re: Some questions regarding scala warm up

Beitrag von nadolskyy »

I have the same problem with appending of lists only with ::

I didn't know yesterday in the morning that ::: is not allowed to use and solved it something easier than your solution looks like:

Code: Alles auswählen

  def flatten[A](xss : List[List[A]]) : List[A] = xss match {
    case Nil => Nil
    case (head :: tail) => head ::: flatten(tail)
  }
My next idea is to use 3 variables and append per each step one element to a result list:
- result list
- list (from which the head will be taken to the result list)
- list
  • with rest lists

    But I didn't try if it works with ::

nadolskyy
Windoof-User
Windoof-User
Beiträge: 24
Registriert: 9. Jul 2010 01:30

Re: Some questions regarding scala warm up

Beitrag von nadolskyy »

this idea works at me (something modified). I have a function for a recursively addition of 2 lists instead of 3 variables which I described before (now it can be solved with 2 variables and this function for addition of two lists). Hope it can help to solve this task :wink:

Talaron
Mausschubser
Mausschubser
Beiträge: 85
Registriert: 26. Apr 2012 11:34

Re: Some questions regarding scala warm up

Beitrag von Talaron »

First of all: Please don't post your solutions here, even if they are not completely finished yet!

About your questions: Especially the flatten task can be solved very elegantly without any additional functions. Remember that pattern matching is very powerful. In an AE-interpreter, you could for example also write cases like "case Add(Num(x), Add(Num(y), Num(z))) => x + y + z". This of course can also be transferred to matching of lists. Alternatively, you can also nest pattern matching if you need to.

yc81reja
Erstie
Erstie
Beiträge: 12
Registriert: 18. Apr 2015 16:25

Re: Some questions regarding scala warm up

Beitrag von yc81reja »

nadolskyy hat geschrieben:this idea works at me (something modified). I have a function for a recursively addition of 2 lists instead of 3 variables which I described before (now it can be solved with 2 variables and this function for addition of two lists). Hope it can help to solve this task :wink:
Thanks for your answer. The error for :: is that both head and tail are deduced as List[A], instead of A so that :: doesn't work due to type mismatch.

yc81reja
Erstie
Erstie
Beiträge: 12
Registriert: 18. Apr 2015 16:25

Re: Some questions regarding scala warm up

Beitrag von yc81reja »

Talaron hat geschrieben:First of all: Please don't post your solutions here, even if they are not completely finished yet!

About your questions: Especially the flatten task can be solved very elegantly without any additional functions. Remember that pattern matching is very powerful. In an AE-interpreter, you could for example also write cases like "case Add(Num(x), Add(Num(y), Num(z))) => x + y + z". This of course can also be transferred to matching of lists. Alternatively, you can also nest pattern matching if you need to.
Sorry for'at. The problem is what I've said, a List[List[A]] is passed as parameter. If we

Code: Alles auswählen

case x :: ss => x ::: flatten(ss) 
it's okay because x is regarded as List[A]. If it is changed to this as

Code: Alles auswählen

case List(x :: List(List(ss))) => x :: flatten(List(ss))
compiler complains aloud for the mismatch as ss is of type A and flatten requires a type List[List[A]] so we can't peel the recursive definition off. I'm just wondering about the approach to do that with only ::

Talaron
Mausschubser
Mausschubser
Beiträge: 85
Registriert: 26. Apr 2012 11:34

Re: Some questions regarding scala warm up

Beitrag von Talaron »

Remember that you can always match lists using the :: operator in the case statement. You don't need any "List(...)" wrapper around it for matching, only for building new lists.

Antworten

Zurück zu „Archiv“