Permalink
Browse files

Eerste versie TargetList, nog zonder predikaten

  • Loading branch information...
1 parent 0004b74 commit 6825b29ea3418b186bc0975e400031714f2ff1f7 @MedeaMelana committed Jul 26, 2012
Showing with 39 additions and 0 deletions.
  1. +39 −0 TargetList.hs
View
@@ -0,0 +1,39 @@
+{-# LANGUAGE GADTs #-}
+{-# LANGUAGE StandaloneDeriving #-}
+
+module TargetList where
+
+import Prelude hiding (length)
+
+import Control.Applicative
+
+data Target = TargetCreature | TargetPlayer
+
+data TargetList t a where
+ Nil :: a -> TargetList t a
+ Snoc :: TargetList t (Target -> a) -> t -> TargetList t a
+
+instance Functor (TargetList t) where
+ fmap f (Nil x) = Nil (f x)
+ fmap f (Snoc xs t) = Snoc (fmap (f .) xs) t
+
+instance Applicative (TargetList t) where
+ pure = Nil
+ xs <*> Nil b = fmap ($ b) xs
+ xs <*> Snoc ys t = Snoc ((.) <$> xs <*> ys) t
+
+collect :: TargetList Target a -> ([Target], a)
+collect (Nil x) = ([], x)
+collect (Snoc xs t) = (t:ts, f t)
+ where
+ (ts, f) = collect xs
+
+length :: TargetList t a -> Int
+length (Nil _) = 0
+length (Snoc xs _) = succ (length xs)
+
+targetCreature :: TargetList () Target
+targetCreature = Snoc (Nil id) ()
+
+targetPlayer :: TargetList () Target
+targetPlayer = Snoc (Nil id) ()

0 comments on commit 6825b29

Please sign in to comment.