Skip to content

Instantly share code, notes, and snippets.

View mitsuji's full-sized avatar

Mitsuji, Takamasa mitsuji

View GitHub Profile
{-# LANGUAGE PackageImports #-}
import Control.Monad
import Control.Applicative ((<$>),(<*>))
import "mtl" Control.Monad.RWS
import qualified System.Random as R
main = do
@mitsuji
mitsuji / while.hs
Last active August 29, 2015 14:26
while loop for ST Monad
{-# LANGUAGE PackageImports #-}
import Control.Monad
import Control.Applicative ((<$>),(<*>))
import Control.Monad.ST
import Data.STRef
import "mtl" Control.Monad.State
main = do
print $ twdst 10
print $ tdwst 10
-- n個の引数を持つ関数が、「1個の引数を持つ、n-1個の引数を持つ関数を返す関数」でもあること
-- haskell ではデフォルトで関数は「関数を返す関数」になっている
-- non curried function ( ordinary in another languages )
foo:: (Int, Int, Int, Int) -> Int
foo (a, j, q, k ) = a + j + q + k
-- curried function ( ordinary in haskell )
bar :: Int -> Int -> Int -> Int -> Int
bar a j q k = a + j + q + k
@mitsuji
mitsuji / gist:99d2f30455dd8a1600c0
Last active August 29, 2015 14:13
Functor, Applicative, Monad
import Data.Functor( (<$>) )
import Control.Applicative( Applicative, (<*>) )
import Prelude hiding( Left, Right )
--
-- 計算の合成(未評価の計算を使った計算)
--
--
-- Functor 単項演算子の適応(map)
import Prelude hiding( Left, Right )
data Heading = North | East | West | South deriving( Show, Read )
type Status = ( (Int, Int), Heading )
data Direction = Forward | Backward | Left | Right deriving( Read )
progress:: Direction -> Status -> Status
progress Forward ((x, y), North) = (( x, y+1 ), North)
progress Backward ((x, y), North) = (( x, y-1 ), South)
progress Left ((x, y), North) = (( x-1, y ), West )
@mitsuji
mitsuji / StopWatchFRP.hs
Last active August 29, 2015 14:12
FRP stop-watch by reactive-banana
{-# LANGUAGE ScopedTypeVariables #-}
module StopWatch where
import System.IO
import Control.Monad (when)
import Control.Concurrent.Timer
import Control.Concurrent.Suspend
import Data.Time.Clock
import Reactive.Banana
@mitsuji
mitsuji / StopWatchIO.hs
Last active August 29, 2015 14:11
non-FRP stop-watch
module StopWatch where
import System.IO
import Control.Monad (when)
import Control.Concurrent.Timer
import Control.Concurrent.Suspend
import Data.Time.Clock
import Data.IORef
@mitsuji
mitsuji / create_vm.sh
Created January 13, 2014 10:24
VirtualBox create vm shell script
#!/bin/sh
# ./create_vm.sh vm1 Debian_64 1024 20000 3393 Downloads/debian-7.1.0-amd64-netinst.iso
# VBoxManage startvm vm1 -type headless
# VBoxManage unregistervm vm1 --delete
VM_NAME=$1
OS_TYPE=$2
MEMORY_SIZE=$3
@mitsuji
mitsuji / gist:1873842
Created February 21, 2012 04:56
Monte Carlo method pi calculation in Haskell
import System.Random.Mersenne( getStdGen, randoms )
import Time
data Point = Point { x::Double, y::Double } deriving( Show )
total = 10000000
main = do
testStart <- getClockTime
gen <- getStdGen
@mitsuji
mitsuji / gist:1629893
Created January 18, 2012 00:00
identon2 check
// file: identon2.cpp
//
// gcc -xc++ -lstdc++ identon2.cpp
// CL /TP /EHsc identon2.cpp
#include <limits.h>
#include <sstream>
#include <iostream>