Missing parameter type

I rewrote a series of Haskell functions in Scala and ran into a compilation error that I cannot explain

The error is as follows:

missing parameter type
    def group[A](xs: List[A]): List[List[A]] = groupBy((a, b) => a == b, xs)
                                                        ^
missing parameter type
    def group[A](xs: List[A]): List[List[A]] = groupBy((a, b) => a == b, xs)
                                                           ^

The code is as follows:

object Stuff {
  def main(args: Array[String]): Unit = {
    val lst = List(1, 1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 6, 7)
    println(group(lst))
  }

  def group[A](xs: List[A]): List[List[A]] = groupBy((a, b) => a == b, xs)
  def groupBy[A](fun: (A, A) => Boolean, input: List[A]): List[List[A]] = // stuff
}

I'm not at all sure what is going on here, why it complains about the missing parameter type. As far as I can see, everything is defined

+3
source share
3 answers

It will compile if you provide an explicit generic argument on the call site groupBy:

def group[A](xs: List[A]): List[List[A]] = groupBy[A]((a, b) => a == b, xs)

See this post for an explanation of why Scala type inference is not suitable for this case.

If you write groupBywith curry argument lists, type information should be output correctly:

def group[A](xs: List[A]): List[List[A]] = groupBy(xs)((a, b) => a == b)

def groupBy[A](input: List[A])(fun: (A, A) => Boolean): List[List[A]] = input match {
  case Nil => Nil
  case (x::xs) =>
    val (ys, zs) = span(fun.curried(x), xs)
    (x::ys)::groupBy(zs)(fun)
}    
+6

. :

def group[A](xs: List[A]): List[List[A]] = groupBy((a: A, b: A) => a == b, xs)
+2

(a, b) => a == b . :

def group[A](xs: List[A]): List[List[A]] = groupBy((a: A, b: A) => a == b, xs)

def group[A](xs: List[A]): List[List[A]] = groupBy[A]((a, b) => a == b, xs)

:

, , . , . , , , ( Scala), . , , . .

+2

Source: https://habr.com/ru/post/1665636/


All Articles