История изменений
Исправление qnikst, (текущая версия) :
не хочу :) ну давай, если быстро получится, то сделаю.
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
class ToList x a where toList :: x -> [a]
instance ToList [a] a where toList = id
instance ToList b a => ToList [b] a where toList = concatMap toList
instance ToList a a where toList a = [a]
data T a = forall x . ToList x a => T x
instance ToList (T a) a where
toList (T x) = toList x
flatten' :: [T a] -> [a]
flatten' = concatMap toList
test1,test2,test3,test4,test5 :: [Int]
test0 = flatten' []
test1 = flatten' [T (1::Int)]
test2 = flatten' [T (1::Int), T (2::Int)]
test3 = flatten' [T [1::Int,2]]
test4 = flatten' [T (1::Int), T [1::Int, 2]]
test5 = flatten' [T [[1::Int]]]
ладно.. тут есть косяк.
Исходная версия qnikst, :
не хочу :) ну давай, если быстро получится, то сделаю.
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
class ToList x a where toList :: x -> [a]
instance ToList [a] a where toList = id
instance ToList b a => ToList [b] a where toList = concatMap toList
instance ToList a a where toList a = [a]
data T a = forall x . ToList x a => T x
instance ToList (T a) a where
toList (T x) = toList x
flatten' :: [T a] -> [a]
flatten' = concatMap toList
test1,test2,test3,test4,test5 :: [Int]
test0 = flatten' []
test1 = flatten' [T (1::Int)]
test2 = flatten' [T (1::Int), T (2::Int)]
test3 = flatten' [T [1::Int,2]]
test4 = flatten' [T (1::Int), T [1::Int, 2]]
test5 = flatten' [T [[1::Int]]]