Skip to content

Instantly share code, notes, and snippets.

@mmitou
mmitou / Matrix.hs
Created October 27, 2013 15:44
行列計算のサンプル
module Data.Matrix where
newtype Matrix a = Matrix {
toList :: [[a]]
} deriving(Eq)
fromList = Matrix
instance Show a => Show (Matrix a) where
show (Matrix []) = ""
@mmitou
mmitou / forest.hs
Created November 2, 2013 09:53
TreeのForestを降りて行くサンプル
{-# LANGUAGE Rank2Types #-}
import Control.Applicative
import Control.Lens
import Control.Lens.Zipper
import Data.List
import Data.Tree
import Data.Tree.Lens
mkTree :: Int -> Tree Int
@mmitou
mmitou / replace.hs
Created November 7, 2013 03:26
継続渡し的なsplit
replace :: Int -> Int -> [a] -> [a]
replace i j xs | 0 <= i && i < j && j < length xs = mkHead . (:) xj . mkBody . (:) xi $ tail
| null xs = []
| otherwise = xs
where
(mkHead, (xi:bodyTail)) = splitCps i xs id
(mkBody, (xj:tail)) = splitCps (j - i - 1) bodyTail id
splitCps :: Int -> [a] -> ([a] -> [a]) -> ([a] -> [a], [a])
splitCps 0 xs f = (f, xs)
@mmitou
mmitou / Counter.hs
Last active December 29, 2015 09:49
DBusのサンプル
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Concurrent
import Control.Monad (forever, when)
import Data.IORef
import Data.Int
import Data.ByteString.Char8
import System.Exit
@mmitou
mmitou / QueueT.hs
Created December 9, 2013 09:52
状態をQueueで管理するモナド instance化する際に、型パラメータを使いたい場合には、FlexibleInstancesを使わなければならない。 instance化する際に、型シノニムを指定したい場合には、TypeSynonymInstancesを使わなければならない。 型シノニムに型パラメータを持たせて宣言した場合に、型シノニムを型として使う箇所では全てのパラメータを埋めなければならない。 例えば以下のように宣言した場合には、 type QueueT e m a = StateT [e] m a このように instance HogeClass e (QueueT e m a) のようにQueueTには必ず3つパラメータを指定する必要がある。 パラメータを3つ指定するのが都合…
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
import Prelude hiding (head)
import Control.Monad.Trans
import Control.Monad.Trans.State
type QueueT e m = StateT [e] m
@mmitou
mmitou / qsort.c
Last active August 29, 2015 13:57
#include <stdio.h>
void print_array(int begin_index, int end_index, int array[]) {
int i;
for(i = begin_index; i <= end_index; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
@mmitou
mmitou / test.c
Created March 25, 2014 03:31
forkの確認
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
/* forkの動作を確認する。 */
/* 親プロセスが死ぬと、子プロセスはinitの子になる事を確認する。 */
int main() {
@mmitou
mmitou / simple.json
Created April 18, 2014 13:01
sshで繋げられなかった
{ "AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "test",
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "AMI" : "ami-2f726546" },
"us-west-1" : { "AMI" : "ami-84f1cfc1" },
"us-west-2" : { "AMI" : "ami-b8f69f88" },
"eu-west-1" : { "AMI" : "ami-a921dfde" },
"ap-southeast-1" : { "AMI" : "ami-787c2c2a" },
"ap-southeast-2" : { "AMI" : "ami-0bc85031" },
@mmitou
mmitou / simple.json
Created April 22, 2014 09:55
sshでつながる
{ "AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "test",
"Parameters" : {
"KeyPairName" : {
"Type" : "String",
"Description" : "key pair name"
}
},
"Mappings" : {
"RegionMap" : {
@mmitou
mmitou / wordCount.go
Created June 14, 2014 06:24
単語数比較
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
var wc map[string]int = make(map[string]int)
for _, key := range strings.Fields(s) {