1

我正在尝试在 Scala 中实现 dropWhile,但在调用“f(h)”错误时出现类型不匹配,该错误表示它实际上找到了它所期望的类型:

def dropWhile[A](l: XList[A])(f: A => Boolean): XList[A] = {

        def dropWhile[A](toCheck: XList[A], toKeep: XList[A]) : XList[A] = toCheck match {
            case XNil => toKeep
            case Cons(h, t) if **f(h)** == false => dropWhile(tail(toCheck), Cons(h, toKeep))
            case Cons(_, Cons(t1, t2)) => dropWhile(Cons(t1, t2), toKeep)
        }

        dropWhile(l, XList[A]())
    }

错误信息:

 found   : h.type (with underlying type A)
[error]  required: A

相关代码:

sealed trait XList[+A] {}
case object XNil extends XList[Nothing]
case class Cons[+A](head: A, tail: XList[A]) extends XList[A]

编辑:

这是一种使其编译的方法-但获胜的答案更好,并解释了原因。

def dropWhile[A](l: XList[A])(f: A => Boolean): XList[A] = {

        @tailrec
        def dropWhile[A](toCheck: XList[A], toKeep: XList[A], dropItem: A => Boolean): XList[A] = toCheck match {
            case Cons(h, XNil) if !dropItem(h) => Cons(h, toKeep)
            case Cons(h, XNil) if dropItem(h) => toKeep
            case Cons(h, t) if !dropItem(h) => dropWhile(t, Cons(h, toKeep), dropItem)
            case Cons(h, t) if dropItem(h) => dropWhile(t, toKeep, dropItem)
        }

        dropWhile(l, XList[A](), f)
    }
4

2 回答 2

4

您已经A在原来的“dropWhile”上有类型参数,它是f. 但是,您随后会在内部引入第二个类型参数,def它会A影响XList. 所以问题是A它们不是同一类型!如果您删除了阴影类型,则一切正常(为使您的代码编译而进行的其他更改很少):

def dropWhile[A](l: XList[A])(f: A => Boolean): XList[A] = {
        def dropWhile(toCheck: XList[A], toKeep: XList[A]) : XList[A] = toCheck match {
            case XNil => toKeep
            case Cons(h, t) if f(h) == false => dropWhile(t, Cons(h, toKeep))
            case Cons(_, Cons(t1, t2)) => dropWhile(Cons(t1, t2), toKeep)
        }
        dropWhile(l, XNil)
    }
于 2013-06-13T15:26:40.713 回答
0

您可以只使用 afoldRight代替(我已将其简化为使用 List 而不是 XList):

scala> def dropWhile[A](l: List[A])(f: A => Boolean): List[A] =
     | l.foldRight(List[A]())((h,t) => if (f(h)) t else h :: t)
dropWhile: [A](l: List[A])(f: A => Boolean)List[A]

scala> val l = List(1,2,3,4,5,6,5,4,3,2,1)
l: List[Int] = List(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1)

scala> l.dropWhile(_ < 4)
res1: List[Int] = List(4, 5, 6, 5, 4, 3, 2, 1)

虽然没有回答你的found : h.type (with underlying type A)问题:-)

于 2015-02-22T13:18:59.843 回答