comparison delta.hs @ 43:90b171e3a73e

Rename to Delta from Similar
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Sat, 01 Nov 2014 15:19:04 +0900
parents similar.hs@b4d3960af901
children 6e270dfe2bb9
comparison
equal deleted inserted replaced
42:1df4f9d88025 43:90b171e3a73e
1 import Control.Applicative
2 import Data.Numbers.Primes -- $ cabal install primes
3
4 data Delta a = Delta [String] a [String] a deriving (Show)
5
6 value :: (Delta a) -> a
7 value (Delta _ x _ _) = x
8
9 similar :: (Delta a) -> a
10 similar (Delta _ _ _ y) = y
11
12 instance (Eq a) => Eq (Delta a) where
13 s == ss = (value s) == (value ss)
14
15 instance Functor Delta where
16 fmap f (Delta xs x ys y) = Delta xs (f x) ys (f y)
17
18 instance Applicative Delta where
19 pure f = Delta [] f [] f
20 (Delta lf f lg g) <*> (Delta lx x ly y) = Delta (lf ++ lx) (f x) (lg ++ ly) (g y)
21
22 mu :: Delta (Delta a) -> Delta a
23 mu (Delta lx (Delta llx x _ _) ly (Delta _ _ lly y)) = Delta (lx ++ llx) x (ly ++ lly) y
24
25 instance Monad Delta where
26 return x = Delta [] x [] x
27 s >>= f = mu $ fmap f s
28
29
30 returnS :: (Show s) => s -> Delta s
31 returnS x = Delta [(show x)] x [(show x)] x
32
33 returnSS :: (Show s) => s -> s -> Delta s
34 returnSS x y = Delta [(show x)] x [(show y)] y
35
36 -- samples
37
38 generator :: Int -> Delta [Int]
39 generator x = let intList = [1..x] in
40 returnS intList
41
42 primeFilter :: [Int] -> Delta [Int]
43 primeFilter xs = let primeList = filter isPrime xs
44 refactorList = filter even xs in
45 returnSS primeList refactorList
46
47 count :: [Int] -> Delta Int
48 count xs = let primeCount = length xs in
49 returnS primeCount
50
51 primeCount :: Int -> Delta Int
52 primeCount x = generator x >>= primeFilter >>= count