関数型プログラミング言語Haskell Part5
■ このスレッドは過去ログ倉庫に格納されています
0643デフォルトの名無しさん
2006/09/07(木) 23:40:27リストに複数の型を押し込めたいのであればExistentially quantified typesが使えます。
ここら辺が参考になります。
http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types#Example:_heterogeneous_lists
http://haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#existential-quantification
http://d.hatena.ne.jp/lethevert/20060902/p1
コードはこんな感じでしょうか。
{-# OPTIONS -fglasgow-exts #-}
class Animal a where
call :: a -> String
data Dog = Dog
instance Animal Dog where
call _ = "bowwow"
data Sparrow = Sparrow
instance Animal Sparrow where
call _ = "cheep"
data AnimalH = forall a. Animal a => AH { a :: a, f :: a -> String }
animals :: [AnimalH]
animals = [AH Sparrow call, AH Dog call, AH Sparrow call]
main = foldr1 (>>) $ map (putStrLn . (\(AH x f) -> f x)) animals
■ このスレッドは過去ログ倉庫に格納されています