Mercurial > hg > Members > atton > similar_monad
annotate similar.hs @ 37:743c05b98dad
Use level in basic/list
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 19 Oct 2014 12:22:54 +0900 |
parents | b4d3960af901 |
children |
rev | line source |
---|---|
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
1 import Control.Applicative |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
2 import Data.Numbers.Primes -- $ cabal install primes |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
3 |
22
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
4 data Similar a = Similar [String] a [String] a deriving (Show) |
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
5 |
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
6 value :: (Similar a) -> a |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
7 value (Similar _ x _ _) = x |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
8 |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
9 similar :: (Similar a) -> a |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
10 similar (Similar _ _ _ y) = y |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
11 |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
12 instance (Eq a) => Eq (Similar a) where |
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
13 s == ss = (value s) == (value ss) |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
14 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
15 instance Functor Similar where |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
16 fmap f (Similar xs x ys y) = Similar xs (f x) ys (f y) |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
17 |
20
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
18 instance Applicative Similar where |
22
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
19 pure f = Similar [] f [] f |
20
d4aa70d94352
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
20 (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.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
21 |
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
22 mu :: Similar (Similar a) -> Similar a |
22
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
23 mu (Similar lx (Similar llx x _ _) ly (Similar _ _ lly y)) = Similar (lx ++ llx) x (ly ++ lly) y |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
24 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
25 instance Monad Similar where |
22
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
26 return x = Similar [] x [] x |
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
27 s >>= f = mu $ fmap f s |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
28 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
29 |
22
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
30 returnS :: (Show s) => s -> Similar s |
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
31 returnS x = Similar [(show x)] x [(show x)] x |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 |
23
b4d3960af901
Define similar constructor for different element
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
22
diff
changeset
|
33 returnSS :: (Show s) => s -> s -> Similar s |
b4d3960af901
Define similar constructor for different element
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
22
diff
changeset
|
34 returnSS x y = Similar [(show x)] x [(show y)] y |
b4d3960af901
Define similar constructor for different element
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
22
diff
changeset
|
35 |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
36 -- samples |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
37 |
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
38 generator :: Int -> Similar [Int] |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
39 generator x = let intList = [1..x] in |
22
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
40 returnS intList |
6 | 41 |
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
42 primeFilter :: [Int] -> Similar [Int] |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
43 primeFilter xs = let primeList = filter isPrime xs |
23
b4d3960af901
Define similar constructor for different element
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
22
diff
changeset
|
44 refactorList = filter even xs in |
b4d3960af901
Define similar constructor for different element
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
22
diff
changeset
|
45 returnSS primeList refactorList |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
46 |
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
47 count :: [Int] -> Similar Int |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
48 count xs = let primeCount = length xs in |
22
f0400c4c953f
Imporve Similar definition. delete "Single" constructor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
49 returnS primeCount |
15 | 50 |
21
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
51 primeCount :: Int -> Similar Int |
af8754322ed4
Define Similar sample
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
52 primeCount x = generator x >>= primeFilter >>= count |