# HG changeset patch # User Yasutaka Higa # Date 1409624869 -32400 # Node ID 6e0285628ead8dae43500b2d6c681d8734abec85 # Parent d30e40d5b5cfafb8ffadc8ab8aef867192233e8c Define similer function diff -r d30e40d5b5cf -r 6e0285628ead similer.hs --- a/similer.hs Tue Sep 02 11:07:10 2014 +0900 +++ b/similer.hs Tue Sep 02 11:27:49 2014 +0900 @@ -13,26 +13,23 @@ mu :: (Eq b) => Similer a (Similer b c) -> Similer b c mu (Similer a f b) = if (eq (f a) b) then b else undefined -double :: Int -> Similer Int Int -double x = Similer (2 * x) id (2 * x) +double :: Int -> Int +double x = (2 * x) -twicePlus :: Int -> Similer Int (Similer Int Int) -twicePlus x = Similer x double (Similer (x + x) id $ x + x) +twicePlus :: Int -> Int +twicePlus x = x + x -plusTwo :: Int -> Similer Int (Similer Int Int) -plusTwo x = Similer x double (Similer (x + 2) id (x + 2)) +plusTwo :: Int -> Int +plusTwo x = x + 2 same :: Eq b => Similer a b -> b same (Similer x f y) = if (f x) == y then y else undefined +similer :: Eq b => (a -> b) -> (a -> b) -> a -> b +similer f g x = same $ Similer x g (f x) + -- samples - -sameExample :: [Int] -sameExample = map same $ map (fmap same) $ fmap twicePlus [1..10] - -nonSameExample :: [Int] -nonSameExample = map same $ map (fmap same) $ fmap plusTwo [1..10] - -nonSameExampleSpecific :: [Int] -nonSameExampleSpecific = map same $ map (fmap same) $ fmap plusTwo [2] +sameExample = map (similer twicePlus double) [1..10] +nonSameExample = map (similer twicePlus plusTwo) [1..10] +nonSameExampleSpecific = map (similer twicePlus plusTwo) [2]