Haskell
ä»æ¥ã¯ãã«ãªã¼åã¨é¢æ°ã®é¨åé©ç¨ã®è©±é¡ã Haskellã®è¦åº§ããScalaã®ã«ãªã¼åã¨é¨åé©ç¨ãè¦ã¦ã¿ã ã¾ããHaskellã§ã®é¢æ°ã®ã«ãªã¼åã¨é¨åé©ç¨ã«ã¤ãã¦ããããã ä¾ãã°ãå¼æ°ãåè¨ããé¢æ° mysum ãããã¨ãã¦ã mysum :: Num a => a -> a -> a -> a myâ¦
doå¼ã§ã®ã¢ããã®è¨è¿°æ¹æ³ã«ã¤ãã¦ç°¡åã«ã¾ã¨ããã Some 1 >>= \x -> Some(x+2) ãdoå¼ã«æ¸ãç´ã㨠apply = do a <- Some 1 Some $ a + 2 Monadã«ã¯returnãããã®ã§ããã«æ¸ãæããã apply = do a <- return 1 :: Option Int return $ a + 2 é¢æ°ã®å¼æ°â¦
Monadã調ã¹ã¦ããã¨ãã¢ãã¢ãè¨ããããªãã¾ããï¼ ãã¦ãOptionãMonad対å¿ããä¾ãæ¸ãã¦ã¿ã¾ãã Monadåã¯ã©ã¹ã¯æ¬¡ã®ãããªå®ç¾©ã«ãªã£ã¦ãã¾ããreturnã¨>>=ãå®è£ ããã¨ãããã¨ãããã class Monad m where return :: a -> m a (>>=) :: m a -> (a â¦
ååã®ç¶ããããMaybeã®ä¸èº«ãMonoidã ã£ãå ´åã®åã¯ã©ã¹ã®å®è£ ã¯æ¬¡ã®ã¨ããã import Data.Monoid data Option a = None | Some a deriving (Eq, Ord, Read, Show) instance (Monoid a) => Monoid (Option a) where mempty = None None `mappend` any = aâ¦
ä»åã¯èªä½ããMaybeç¨ã®Monoidåã¯ã©ã¹ã®å®è£ ãä½ã£ã¦ã¿ã¾ãã Monoidã£ã¦ä½ã£ã¦ãã£ã±ãããããªãã®ã§ãåã¯ã©ã¹ã®å®ç¾©ãè¦ã¦ã¿ã¾ãã class Monoid m where mempty :: m mappend :: m -> m -> m mconcat :: [m] -> m mconcat = foldr mappend mempty mcoâ¦
é£ç¶æ稿ã§ããããããHæ¬ãèªã¿ãªããããµã¨ã¢ã¤ãã¢ã湧ãã¦ããã®ã§Haskellã³ã¼ããæ¸ããªãã£ã¦ã¿ãã ãããHaskellãã®ããå¦ã¼ãï¼ä½è : Miran Lipovacaåºç社/ã¡ã¼ã«ã¼: ãªã¼ã 社çºå£²æ¥: 2012/09/21ã¡ãã£ã¢: Kindleçè³¼å ¥: 1人 ã¯ãªãã¯: 4åãã®â¦
Maybeã®èªä½ã§ãããååã«å¼ãç¶ãApplicativeåã¯ã©ã¹ã®å®è£ ã追å ãã¦ã¿ã¾ããã ã³ã¼ãã¯æ¬¡ã®ã¨ããã import Control.Applicative data Option a = None | Some a deriving (Eq, Show) instance Functor Option where fmap f (Some x) = Some (f x) fmaâ¦
ã¢ãããç解ããããã«Maybeãèªä½ãããã¨ã«ããã®ã§ãé©å½ã«ã³ã¼ããããã¦ã¿ããã¢ããå¼·è ã®æ¹ã®ãã³ããããã°ãããã³ã³ãã³ãåããã®ã§ã¯æã data Option a = None | Some a deriving (Eq, Show) instance Functor Option where fmap f (Some x) =â¦