Warum akzeptiert Haskell meine kombinatorische "zip" -Definition nicht?

Dies ist die Lehrbuch-Zip-Funktion:

zip :: [a] -> [a] -> [(a,a)]
zip [] _ = []
zip _ [] = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys

Ich habe vorher bei #haskell gefragt, ob "zip" allein mit "foldr" implementiert werden kann, keine Rekursion, kein Pattern Matching. Nach einigem Überlegen ist uns aufgefallen, dass die Rekursion durch Fortsetzungen beseitigt werden kann:

zip' :: [a] -> [a] -> [(a,a)]
zip' = foldr cons nil
    where
        cons h t (y:ys) = (h,y) : (t ys)
        cons h t []     = []
        nil             = const []

Wir haben immer noch die Mustererkennung. Nach einigem weiteren Anstoßen mit Neuronen fand ich eine unvollständige Antwort, die ich für logisch hielt:

zip :: [a] -> [a] -> [a]
zip a b = (zipper a) (zipper b) where
    zipper = foldr (\ x xs cont -> x : cont xs) (const [])

It gibt eine flache Liste zurück, führt jedoch das Komprimieren durch. Ich war mir sicher, dass es Sinn machte, aber Haskell beklagte sich über den Typ. Ich fuhr fort, es auf einem untypisierten Lambda-Rechner zu testen, und es funktionierte. Warum kann Haskell meine Funktion nicht akzeptieren?

Der Fehler ist:

zip.hs:17:19:
    Occurs check: cannot construct the infinite type:
      t0 ~ (t0 -> [a]) -> [a]
    Expected type: a -> ((t0 -> [a]) -> [a]) -> (t0 -> [a]) -> [a]
      Actual type: a
                   -> ((t0 -> [a]) -> [a]) -> (((t0 -> [a]) -> [a]) -> [a]) -> [a]
    Relevant bindings include
      b ∷ [a] (bound at zip.hs:17:7)
      a ∷ [a] (bound at zip.hs:17:5)
      zip ∷ [a] -> [a] -> [a] (bound at zip.hs:17:1)
    In the first argument of ‘foldr’, namely ‘cons’
    In the expression: ((foldr cons nil a) (foldr cons nil b))

zip.hs:17:38:
    Occurs check: cannot construct the infinite type:
      t0 ~ (t0 -> [a]) -> [a]
    Expected type: a -> (t0 -> [a]) -> t0 -> [a]
      Actual type: a -> (t0 -> [a]) -> ((t0 -> [a]) -> [a]) -> [a]
    Relevant bindings include
      b ∷ [a] (bound at zip.hs:17:7)
      a ∷ [a] (bound at zip.hs:17:5)
      zip ∷ [a] -> [a] -> [a] (bound at zip.hs:17:1)
    In the first argument of ‘foldr’, namely ‘cons’
    In the fourth argument of ‘foldr’, namely ‘(foldr cons nil b)’

Antworten auf die Frage(6)

Ihre Antwort auf die Frage