Skip to content

Instantly share code, notes, and snippets.

@takeouchida
takeouchida / json.hs
Created September 9, 2011 15:03
Tiny JSON Parser
import Control.Applicative ((<$>), (<$), (<*>), (<*), (*>))
import Prelude hiding (null)
import Text.Parsec hiding (string)
import Text.Parsec.Language (javaStyle)
import qualified Text.Parsec.Token as P
data Value = S String | N Double | O [(String, Value)] | A [Value] | B Bool | Null deriving Show
lexer = P.makeTokenParser javaStyle
symbol = P.symbol lexer
@takeouchida
takeouchida / Lookup.hs
Created December 3, 2011 05:32
An example of missing some kinds of type constraints.
module Lookup where
import Prelude hiding (lookup)
lookup :: a -> [(a, b)] -> Maybe b -- valid if the type a is a kind of Eq instances
lookup k [] = Nothing
lookup k ((k', v):ls) = if k == k' then Just v else lookup k ls
@takeouchida
takeouchida / state.dump-asm
Created February 26, 2012 15:33
A code snippet using Strict State Monad
_Main_mainzuzdszdwa_info:
Lc2q2:
addq $40,%r12
cmpq 144(%r13),%r12
ja Lc2q7
cmpq $100,%r14
jg Lc2qb
movq %rsi,%rax
addq %r14,%rax
incq %r14
@takeouchida
takeouchida / div2_double.v
Created March 26, 2012 15:19
x * 2 / 2 = x
Require Import Div2.
Theorem div2_double : forall x, div2 (double x) = x.
intro x.
induction x.
reflexivity.
unfold double.
unfold double in IHx.
rewrite plus_Sn_m.
@takeouchida
takeouchida / tetroid.hs
Created October 6, 2012 09:50
Tetroid recognition
module Main where
import Control.Monad (mapM_)
import Control.Arrow ((***))
import Data.List (find, sort)
import Data.Maybe (maybe)
import Test.HUnit (Test(..), runTestTT, (~=?))
findTetroid :: [Int] -> Char
findTetroid = match . normalize . toCoord
@takeouchida
takeouchida / maybe.hs
Created November 11, 2012 13:51
A free monad example: converting a kind of functor instance to monad
module Main where
import Control.Monad.Free
data Option a = Some a | None
deriving Show
instance Functor Option where
fmap f (Some a) = Some (f a)
fmap f None = None
@takeouchida
takeouchida / chairs.hs
Created February 2, 2013 09:53
第7回オフラインリアルタイムどう書くの回答です。
module Main where
import Control.Arrow (first)
import Data.Char (isUpper, isLower, toUpper)
import Data.List (tails, maximumBy, findIndex)
import Data.Ord (comparing)
import Data.Maybe (fromMaybe)
import Test.HUnit (Test(..), runTestTT, (~=?))
type Chair = Maybe Char
package sandbox;
import java.util.HashSet;
import java.util.Set;
enum 音 {
猫の鳴き声, 犬の鳴き声, ツクツクホウシの鳴き声 // などなど...
}
interface 聞く者 {
@takeouchida
takeouchida / helloworld.hs
Created November 27, 2013 00:57
"Hello World" in Haskell without any numeric, character, or string literals.
o = length $ return undefined
s = succ
d n = n + n
main = putStrLn $ map toEnum
[ d $ d $ d $ s $ d $ d $ d o
, s $ d $ d $ s $ d $ d $ d $ s $ d o
, d $ d $ s $ d $ s $ d $ d $ s $ d o
, d $ d $ s $ d $ s $ d $ d $ s $ d o
, s $ d $ s $ d $ s $ d $ s $ d $ d $ s $ d o
, d $ d $ d $ d $ d o
@takeouchida
takeouchida / reduce.v
Last active December 29, 2015 16:49
Monoid type class and its instances.
Require Import List Arith ZArith Program.
Class Monoid {A} (dot : A -> A -> A) (zero : A) := {
monoid_1st_law : forall a, dot a zero = a;
monoid_2nd_law : forall a, dot zero a = a;
monoid_3rd_law : forall a b c, dot (dot a b) c = dot a (dot b c)
}.
Instance nat_plus_Monoid : Monoid plus 0.
split; auto with arith.