そのフォーラムでの活動がはるかに少ないため、これはコースラ関数型プログラミングコースからのクロスポストです。
私は次のコードを書きました(宿題なので部品は編集されています):
type Occurrences = List[(Char, Int)]
def subtract(x: Occurrences, y: Occurrences): Occurrences = {
val mx: Map[Char, Int] = x toMap
y.foldLeft (redacted) (redacted => simple expression using updated and -)) toList
}
これにより、次のコンパイルエラーが発生します。
type mismatch; found : Map[Char,Int] required: <:<[(Char, Int), (?, ?)]
ただし、valステートメントを介してtoListなしで3行目のコピーを追加すると、エラーはなくなります。
type Occurrences = List[(Char, Int)]
def subtract(x: Occurrences, y: Occurrences): Occurrences = {
val mx: Map[Char, Int] = x toMap
val foo: Map[Char, Int] = y.foldLeft (redacted) (redacted => simple expression using updated and -))
y.foldLeft (redacted) (redacted => simple expression using updated and -)) toList
}
これは、タイプチェッカーに何らかの追加のヒントを与えることと関係があると思いますが、なぜこれが発生するのかを具体的に知っている人はいますか?