2016-11-04 5 views
1

Scalaの新機能です。練習として、ガード付きタプルのリストにマッチステートメントを書くようにしています。私はマップが問題を解決するだろうが、私はパターンマッチングの理解を得ようとしていることを知っています。Scalaマッチガードを持つタプルのリスト

私は引数としてList[(Char, Int)]をとる関数を書くようにしています。この関数はエントリをソートし、2つのエントリが同じキー値を持つ場合、それらは一緒に追加されます。したがって、次の引数List(('q', 1'), ('a', 1), ('c', 2), ('a', 2), ('c', 1))List(('a', 3), ('c', 3'), ('q', 1))になります。

私は次のコードが付属しています:

def sortAndAggregateList(chars: List[(Char, Int)]) : List[(Char, Int)] = { 
    chars match { 
    case (charP1, numP1) :: (charP2, numP2) :: (x : List[(String, Int)]) if (charP1 > charP2) => 
     sortAndAggregateList((charP2, numP2) :: (charP1, numP1) :: x) 
    case (charP1, numP1) :: (charP2, numP2) :: (x : List[(String, Int)]) if (charP1 < charP2) => 
     sortAndAggregateList((charP1, numP1) :: (charP2, numP2) :: x) 
    case (charP1, numP1) :: (charP2, numP2) :: (x : List[(String, Int)]) if (charP1 == charP2) => 
     sortAndAggregateList((charP1, numP1 + numP2) :: x) 
    case Nil => 
     Nil 
    } 
} 

しかし、私は次の警告を得る:

:14:警告:無益なタイプのテスト:List型の値[(シャア、int)を]リスト[(String、Int)](リスト[(String、Int)])の根底にあるListでもかまいません。 xAnyのエラーです。

提案がありますか?

+0

を削除したくない場合は、エラー・メッセージを参照してください:あなたは '' chars'に対して一致していますList [(Char、Int)] 'であるが、パターンでは' x 'は 'List [(String、Int)]'であると期待する。 – Josef

答えて

2

エラーは、それぞれのcase文(:List [(String、Int)])の後に行うタイプチェックです。

その後
def sortAndAggregateList(chars: List[(Char, Int)]) : List[(Char, Int)] = { 
    chars match { 
    case (charP1, numP1) :: (charP2, numP2) :: x if (charP1 > charP2) => 
     sortList(p1 :: p2 :: x) 
    case (charP1, numP1) :: (charP2, numP2) :: x if (charP1 < charP2) => 
     sortList(p2 :: p1 :: x) 
    case (charP1, numP1) :: (charP2, numP2) :: x if (charP1 == charP2) => 
     val p3: (Char, Int) = (charP1, numP1 + numP2) 
     sortList(p3 :: x) 
    case x => 
     x 
    case Nil => 
     Nil 
    } 
} 

あなたはコンパイラがP1およびP2が未定義であることを示していますことを見つける:あなたは、エラーが消え、次のようにコードを変更した場合

。これを修正するには、p1 =(charP1、numP1)とp2 =(charP2、numP2)として設定する必要があります。あなたは次の操作を行うことができ、あなたの構文を使用してこの問題を解決するには :

def sortAndAggregateList(chars: List[(Char, Int)]) : List[(Char, Int)] = { 
    chars match { 
    case (charP1, numP1) :: (charP2, numP2) :: x if (charP1 > charP2) => 
     sortList((charP1, numP1) :: (charP2, numP2) :: x) 
    case (charP1, numP1) :: (charP2, numP2) :: x if (charP1 < charP2) => 
     sortList((charP2, numP2) :: (charP1, numP1) :: x) 
    case (charP1, numP1) :: (charP2, numP2) :: x if (charP1 == charP2) => 
     val p3: (Char, Int) = (charP1, numP1 + numP2) 
     sortList(p3 :: x) 
    case x => 
     x 
    case Nil => 
     Nil 
    } 
} 

今だけミッシングリンクを追加していませんでしたsortlistが機能です。 私はケースを考えるので、私はこれが機能するかどうかわからない:

case x => x 

は次のようになります。

case x :: Nil => x :: Nil 

そうでない場合、xが何もマッチします。これはまた、あなたのケースを削除する可能性を残し:

case Nil => Nil 

あなたはケースのx => X

+0

この' case x => x'は動作します – pamu

+0

はい、あなたは正しいそれが、それがNilでも何でもマッチすると付け加えた理由です。 – biro

+0

case Nil => Nilは冗長です – pamu

0

x以降の余分な型注釈は必要ではなく、間違っています。

(強制ではありません。あなたは型注釈を省略することができます)を使用

(x : List[(Char, Int)]) 

コンプリート機能を

def sortAndAggregateList(chars: List[(Char, Int)]): List[(Char, Int)] = chars match { 
    case (charP1, numP1) :: (charP2, numP2) :: x if charP1 > charP2 => 

     sortAndAggregateList((charP2, numP2) :: (charP1, numP1) :: x) 

    case (charP1, numP1) :: (charP2, numP2) :: x if charP1 < charP2 => 

     sortAndAggregateList((charP1, numP1) :: (charP2, numP2) :: x) 

    case (charP1, numP1) :: (charP2, numP2) :: x if charP1 == charP2 => 

     sortAndAggregateList((charP1, numP1 + numP2) :: x) 

    case x => x 
    } 

コードがあればはるかにきれいになり、代わりにこの

(x : List[(String, Int)]) 

を削除あなたはタプルを折りたたむことを検討します

def sortAndAggregateList(chars: List[(Char, Int)]): List[(Char, Int)] = chars match { 
    case a :: b :: x if a._1 > b._2 => 

     sortAndAggregateList(b :: a :: x) 

    case a :: b :: x if a._1 < b._1 => 

     sortAndAggregateList(a :: b :: x) 

    case a :: b :: x if a._1 == b._1 => 

     sortAndAggregateList((a._1, (a._2 + b._2)) :: x) 

    case x => x 

    } 

ケースcase x => xは、Nilケースと1つの要素ケースを持つリストの両方に一致します。

+0

この機能には弱点があります。リスト(( "" t "、1)) – biro

+0

@biro提案してくれてありがとう – pamu

関連する問題