1

これはおそらくばかげた質問であり、確かに最良のコードではありませんが、入力情報を追加すると、haskell が私に怒鳴る理由がよくわかりません

これは機能しません (x:a の 3 行目に注意してください):

groupBy3 :: (a -> a -> Bool)-> [a] -> [[a]]
groupBy3 eq = foldr step []
    where step (x:a) res =
            let (used, res2) = foldr insertel (False, []) res
                    where insertel (al) (used, acc) =
                            if used then (True, al:acc)
                            else if eq x (head al) then
                                (True, (x:al):acc)
                            else
                                (False, al:acc)
            in
              if used then
                  res2
              else [x]:res

これは機能しますが(3行目に x の型注釈がないことに注意してください)

groupBy3 :: (a -> a -> Bool)-> [a] -> [[a]]
groupBy3 eq = foldr step []
    where step x res =
            let (used, res2) = foldr insertel (False, []) res
                    where insertel (al) (used, acc) =
                            if used then (True, al:acc)
                            else if eq x (head al) then
                                (True, (x:al):acc)
                            else
                                (False, al:acc)
            in
              if used then
                  res2
              else [x]:res
4

2 に答える 2

6

Sebastian Redl の回答に追加するには: 標準 Haskell の関数本体の最上位定義から型変数を参照することはできません。ただし、それを可能にする「Scoped type variables」と呼ばれる GHC 拡張があります ( http://www.haskell.org/haskellwiki/Scoped_type_variablesを参照) 。

于 2013-09-20T14:21:18.393 に答える
5

(x:a)情報を入力するのではなく、リスト コンストラクターでのパターン マッチであり、リストの先頭を に入れ、x残りを に入れaます。

入力情報には , が使用され、メイン関数の型のようなプレースホルダーは句に表示されない::ため、とにかくここでは意味がありません。それは独立した型識別子になるため、意味がありません。awhere

于 2013-09-20T14:13:11.737 に答える