2015-06-16 51 views
1

我正在通过一些练习:斯卡拉函数式编程特别是问题5.2。问题是,我用答案关键字拼凑了以下代码。斯卡拉错误的前向参考

sealed trait Stream[+A] 
{ 
    def take(n: Int): Stream[A] = this match { 
    case Cons(hs, ts) if n > 1 => cons(h(), t().take(n - 1)) 
    case Cons(hs, _) if n == 1 => cons(h(), empty) 
    case _ => empty 
    } 

} 
case object Empty extends Stream[Nothing] 
case class Cons[+A](h:() => A, t:() => Stream[A]) extends Stream[A] 

object Stream{ 
    def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = { 
    lazy val head = hd 
    lazy val tail = tl 
    Cons(() => head ,() => tail) 
    } 

    def empty[A]: Stream[A] = Empty 

    def apply[A](as: A*): Stream[A] = 
    if (as.isEmpty) empty 
    else cons(as.head, apply(as.tail: _*)) 

} 

我得到的REPL如下:

<console>:10: error: not found: type A 
        def take(n: Int): Stream[A] = this match { 
              ^
<console>:11: error: not found: value Cons 
         case Cons(hs, ts) if n > 1 => cons(h(), t().take(n - 1)) 
         ^
<console>:11: error: not found: value cons 
         case Cons(hs, ts) if n > 1 => cons(h(), t().take(n - 1)) 
                ^
<console>:12: error: not found: value Cons 
         case Cons(hs, _) if n == 1 => cons(h(), empty) 
         ^
<console>:12: error: not found: value cons 
         case Cons(hs, _) if n == 1 => cons(h(), empty) 
                ^
<console>:13: error: not found: value empty 
         case _ => empty 

          ^

回答

1

你有这个代码2个问题:

  1. 没有明确规定该emptycons方法位于伴侣物体Stream

为了解决这个问题,你需要或者import Stream._到类:

sealed trait Stream[+A] { 
    import Stream._ 
    def take(n: Int): Stream[A] = this match { 
    case Cons(hs, ts) if n > 1 => cons(hs(), ts().take(n - 1)) 
    case Cons(hs, _) if n == 1 => cons(hs(), empty) 
    case _ => empty 
    } 
} 

或者你需要明确指定:

sealed trait Stream[+A] { 
    def take(n: Int): Stream[A] = this match { 
    case Cons(hs, ts) if n > 1 => Stream.cons(hs(), ts().take(n - 1)) 
    case Cons(hs, _) if n == 1 => Stream.cons(hs(), Stream.empty) 
    case _ => Stream.empty 
    } 
} 
  • 使用的t变量名和h,它们在case class Cons而不是hsts的绑定变量中。
  • 当你这样做:

    case Cons(hs, ts) if n > 1 => Stream.cons(hs(), ts().take(n - 1)) 
    case Cons(hs, _) if n == 1 => Stream.cons(hs(), Stream.empty) 
    

    你是说你要提取的情况下类参数hsts分别在接下来的代码块中使用它们。无论他们在案例类别中是否被称为ht,它们都将被分配您在匹配中指定的名称。

    修复这两个问题,你的代码应该编译(我个人使用Scala 2.11.5和Java 1.7测试,但我不认为它应该的问题):

    sealed trait Stream[+A] { 
        def take(n: Int): Stream[A] = this match { 
        case Cons(hs, ts) if n > 1 => Stream.cons(hs(), ts().take(n - 1)) 
        case Cons(hs, _) if n == 1 => Stream.cons(hs(), Stream.empty) 
        case _ => Stream.empty 
        } 
    } 
    case object Empty extends Stream[Nothing] 
    case class Cons[+A](h:() => A, t:() => Stream[A]) extends Stream[A] 
    
    object Stream{ 
        def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = { 
        lazy val head = hd 
        lazy val tail = tl 
        Cons(() => head ,() => tail) 
        } 
    
        def empty[A]: Stream[A] = Empty 
    
        def apply[A](as: A*): Stream[A] = 
        if (as.isEmpty) empty 
        else cons(as.head, apply(as.tail: _*)) 
    
    } 
    
    +0

    谢谢!我正在使用intellij,并且导入效果很好,但显式指定对象时虽然两者都值得用REPL。我认为变量名称在我试图伸手去拿秸秆的时候就会出现一些错误......可以这么说。 –

    +0

    嗯,这很奇怪,对象表示法不起作用。也许还有其他类干扰?也许你已经导入了'scala.collection.Stream'?如果我到这里并粘贴这段代码,那么它编译得很好:http://www.tutorialspoint.com/compile_scala_online.php –