Skip to content

Instantly share code, notes, and snippets.

import Data.Monoid
main = do
print $ LT `mappend` GT
print $ GT `mappend` LT
print $ mempty `mappend` LT
print $ mempty `mappend` GT
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
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)
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")
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")
main = do
print $ (3 * 2) * (8 * 5)
print $ 3 * (2 * (8 * 5))
print $ "la" ++ ("di" ++ "ga")
print $ ("la" ++ "di") ++ "ga"
-- data CoolBool = CoolBool { getCoolBool :: Bool }
newtype CoolBool = CoolBool { getCoolBool :: Bool }
helloMe :: CoolBool -> String
helloMe (CoolBool _ ) = "hello"
main = do
print $ helloMe undefined
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]]
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)
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