Mercurial > hg > Members > atton > delta_monad
annotate similar.hs @ 21:af8754322ed4
Define Similar sample
author  Yasutaka Higa <e115763@ie.uryukyu.ac.jp> 

date  Tue, 23 Sep 2014 17:27:11 +0900 
parents  d4aa70d94352 
children  f0400c4c953f 
rev  line source 

17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
16
diff
changeset

1 import Control.Applicative 
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

2 import Data.Numbers.Primes  $ cabal install primes 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

3 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

4 data Similar a = Single [String] a  Similar [String] a [String] a deriving (Show) 
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
16
diff
changeset

5 
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

6 value :: (Similar a) > a 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

7 value (Single _ x) = x 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

8 value (Similar _ x _ _) = x 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

9 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

10 similar :: (Similar a) > a 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

11 similar (Single _ x) = x 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

12 similar (Similar _ _ _ y) = y 
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
13
diff
changeset

13 
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
13
diff
changeset

14 instance (Eq a) => Eq (Similar a) where 
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

15 s == ss = (value s) == (value ss) 
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

16 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

17 instance Functor Similar where 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

18 fmap f (Single xs x) = Single xs (f x) 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

19 fmap f (Similar xs x ys y) = Similar xs (f x) ys (f y) 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

20 
20
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
19
diff
changeset

21 instance Applicative Similar where 
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
19
diff
changeset

22 pure = Single [] 
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
19
diff
changeset

23 (Single lf f) <*> (Single lx x) = Single (lf ++ lx) (f x) 
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
19
diff
changeset

24 (Single lf f) <*> (Similar lx x ly y) = Similar (lf ++ lx) (f x) (lf ++ ly) (f y) 
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
19
diff
changeset

25 (Similar lf f lg g) <*> (Single lx x) = Similar (lf ++ lx) (f x) (lg ++ lx) (g x) 
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
19
diff
changeset

26 (Similar lf f lg g) <*> (Similar lx x ly y) = Similar (lf ++ lx) (f x) (lg ++ ly) (g y) 
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
19
diff
changeset

27 
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

28 mu :: Similar (Similar a) > Similar a 
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

29 mu (Single ls (Single lx x)) = Single (ls ++ lx) x 
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

30 mu (Single ls (Similar lx x ly y)) = Similar (ls ++ lx) x (ls ++ ly) y 
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

31 mu (Similar lx (Single llx x) ly (Single lly y)) = Similar (lx ++ llx) x (ly ++ lly) y 
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

32 mu (Similar lx (Similar llx x _ _) ly (Similar _ _ lly y)) = Similar (lx ++ llx) x (lx ++ lly) y 
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

33 mu _ = error "Invalid Similar" 
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

34 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

35 instance Monad Similar where 
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

36 return = Single [] 
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
18
diff
changeset

37 s >>= f = mu $ fmap f s 
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

38 
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
17
diff
changeset

39 
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
10
diff
changeset

40 
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
diff
changeset

41 
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
15
diff
changeset

42  samples 
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
2
diff
changeset

43 
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

44 generator :: Int > Similar [Int] 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

45 generator x = let intList = [1..x] in 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

46 Single [(show intList)] intList 
6  47 
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

48 primeFilter :: [Int] > Similar [Int] 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

49 primeFilter xs = let primeList = filter isPrime xs 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

50 refactorList = filter even xs in 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

51 Similar [(show primeList)] primeList [(show refactorList)] refactorList 
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
10
diff
changeset

52 
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

53 count :: [Int] > Similar Int 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

54 count xs = let primeCount = length xs in 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

55 Single [(show primeCount)] primeCount 
15  56 
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

57 primeCount :: Int > Similar Int 
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.uryukyu.ac.jp>
parents:
20
diff
changeset

58 primeCount x = generator x >>= primeFilter >>= count 