I have once again discovered a Scala “feature” which I had forgotten, (perhaps forgotten and re-learned several times). I’d like to know whether this is considered a feature of a bug in the Scala compiler.
The following definition of every
works.
def every[A,B](L1:List[A],L2:List[B])(f:(A,B)=>Boolean):Boolean = {
(L1,L2) match {
case (a::as,b::bs) => f(a,b) && every(as,bs)(f)
case (_,_) => true
}
}
assert( every(List(10,20,30),List(1,2,3))(_>_))
But neither of the following works, because the compiler cannot figure out the type of _>_
.
def xevery[A,B](f:(A,B)=>Boolean,L1:List[A],L2:List[B]):Boolean = {
(L1,L2) match {
case (a::as,b::bs) => f(a,b) && xevery(f,as,bs)
case (_,_) => true
}
}
assert( xevery(_>_,List(10,20,30),List(1,2,3))) // compiler complains about type of _>_
def yevery[A,B](f:(A,B)=>Boolean)(L1:List[A],L2:List[B]):Boolean = {
(L1,L2) match {
case (a::as,b::bs) => f(a,b) && yevery(f)(as,bs)
case (_,_) => true
}
}
assert( yevery(_>_)(List(10,20,30),List(1,2,3))) // compiler complains about type of _>_