## Mercurial > hg > Members > atton > delta_monad

### view similar.hs @ 23:b4d3960af901

Find changesets by keywords (author, files, the commit message), revision
number or hash, or revset expression.

Define similar constructor for different element

author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|

date | Tue, 30 Sep 2014 14:10:26 +0900 |

parents | f0400c4c953f |

children |

line wrap: on

line source

import Control.Applicative import Data.Numbers.Primes -- $ cabal install primes data Similar a = Similar [String] a [String] a deriving (Show) value :: (Similar a) -> a value (Similar _ x _ _) = x similar :: (Similar a) -> a similar (Similar _ _ _ y) = y instance (Eq a) => Eq (Similar a) where s == ss = (value s) == (value ss) instance Functor Similar where fmap f (Similar xs x ys y) = Similar xs (f x) ys (f y) instance Applicative Similar where pure f = Similar [] f [] f (Similar lf f lg g) <*> (Similar lx x ly y) = Similar (lf ++ lx) (f x) (lg ++ ly) (g y) mu :: Similar (Similar a) -> Similar a mu (Similar lx (Similar llx x _ _) ly (Similar _ _ lly y)) = Similar (lx ++ llx) x (ly ++ lly) y instance Monad Similar where return x = Similar [] x [] x s >>= f = mu $ fmap f s returnS :: (Show s) => s -> Similar s returnS x = Similar [(show x)] x [(show x)] x returnSS :: (Show s) => s -> s -> Similar s returnSS x y = Similar [(show x)] x [(show y)] y -- samples generator :: Int -> Similar [Int] generator x = let intList = [1..x] in returnS intList primeFilter :: [Int] -> Similar [Int] primeFilter xs = let primeList = filter isPrime xs refactorList = filter even xs in returnSS primeList refactorList count :: [Int] -> Similar Int count xs = let primeCount = length xs in returnS primeCount primeCount :: Int -> Similar Int primeCount x = generator x >>= primeFilter >>= count