2015-06-16 65 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 –