This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Monoid | |
| main = do | |
| print $ LT `mappend` GT | |
| print $ GT `mappend` LT | |
| print $ mempty `mappend` LT | |
| print $ mempty `mappend` GT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Monoid | |
| -- newtypeにくるむ | |
| -- この方法は面倒なので、orとかandとか使ったほうがいい | |
| main = do | |
| print $ getAny $ Any True `mappend` Any False | |
| print $ getAny $ mempty `mappend` Any True | |
| print $ getAny . mconcat . map Any $ [False, False, False, True] | |
| print $ getAny $ mempty `mappend` mempty |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Monoid | |
| -- getProductなどでモノイドの中身の値を取り出す | |
| main = do | |
| -- product | |
| print $ Product 3 `mappend` Product 9 | |
| print $ getProduct $ Product 3 `mappend` Product 9 | |
| print $ getProduct $ Product 3 `mappend` mempty | |
| print $ getProduct $ Product 3 `mappend` Product 4 `mappend` Product 2 | |
| print $ getProduct $ Product 3 `mappend` (Product 4 `mappend` Product 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Monoid | |
| -- instance Monoid [a] where | |
| -- mempty = [] | |
| -- mappend = (++) | |
| main = do | |
| print $ [1,2,3] `mappend` [4,5,6] | |
| print $ ("one" `mappend` "two") `mappend` "tree" | |
| print $ "one" `mappend` ("two" `mappend` "tree") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Monoid | |
| -- instance Monoid [a] where | |
| -- mempty = [] | |
| -- mappend = (++) | |
| main = do | |
| print $ [1,2,3] `mappend` [4,5,6] | |
| print $ ("one" `mappend` "two") `mappend` "tree" | |
| print $ "one" `mappend` ("two" `mappend` "tree") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| main = do | |
| print $ (3 * 2) * (8 * 5) | |
| print $ 3 * (2 * (8 * 5)) | |
| print $ "la" ++ ("di" ++ "ga") | |
| print $ ("la" ++ "di") ++ "ga" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- data CoolBool = CoolBool { getCoolBool :: Bool } | |
| newtype CoolBool = CoolBool { getCoolBool :: Bool } | |
| helloMe :: CoolBool -> String | |
| helloMe (CoolBool _ ) = "hello" | |
| main = do | |
| print $ helloMe undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Applicative | |
| sequenceA :: (Applicative f) => [f a] -> f [a] | |
| sequenceA = foldr (liftA2 (:)) (pure []) | |
| main = do | |
| print $ sequenceA [Just 1, Just 2] | |
| print $ sequenceA [Just 3, Just 2, Just 1] | |
| print $ sequenceA [Just 3, Nothing, Just 1] | |
| print $ sequenceA [[1,2,3],[4,5,6]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.language.implicitConversions | |
| // R is derived from Rational with some fixes. | |
| // https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6 | |
| case class R(numer: Int, denom: Int) extends Ordered[R] { | |
| def reduce: R = { | |
| def gcd(x: Int, y: Int): Int = { | |
| if (x == 0) y | |
| else if (x < 0) gcd(-x, y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scalaz._ | |
| import Scalaz._ | |
| object Gcd { | |
| def main(args: Array[String]): Unit = { | |
| def gcd(a: Int, b: Int): Writer[List[String], Int] = | |
| if (b == 0) for { | |
| _ <- List("Finished with " + a.shows).tell | |
| } yield a | |
| else |