I'd like to implement my list in Haskell.
But I can't do this. The implementation of <*>
causes
data List a = a :+ (List a) | Empty deriving Show
infixr 9 :+
instance Functor List where
fmap _ Empty = Empty
fmap f (a :+ xs) = f a :+ fmap f xs
instance Applicative List where
pure x = x :+ Empty
Empty <*> _ = Empty
(f :+ fs) <*> xs = fmap f xs :+ (fs <*> xs) -- Error
main :: IO ()
main = do
print $ 1 :+ 2 :+ 3 :+ Empty
print $ fmap (^2) (1 :+ 2 :+ 3 :+ Empty)
print $ ((+1) :+ (*2) :+ (^2) :+ Empty) <*> (1 :+ 2 :+ 3 :+ Empty)
The error is
Couldn't match expected type ‘b’ with actual type ‘List b’ ‘b’ is a rigid type variable bound by ...
:+
constructor to concatenate two lists. – zaquest