これはおそらくばかげた質問であり、確かに最良のコードではありませんが、入力情報を追加すると、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