Skip to content

Instantly share code, notes, and snippets.

View mitsuji's full-sized avatar

Mitsuji, Takamasa mitsuji

View GitHub Profile
@mitsuji
mitsuji / expected_value.kk
Created June 19, 2022 02:53
Expected Value in koka
effect flip
ctl flip () : bool
fun calculation ()
if flip () then
val unused = flip ()
if flip () then 0.5
else 4.0
else 1.0
@mitsuji
mitsuji / BWord.hs
Last active May 24, 2019 12:32
Word type supports Underflow/Overflow error
module Data.BWord(BWord(..),fromBWord) where
newtype BWord = BWord Integer
deriving (Eq,Ord,Show)
fromBWord :: BWord -> Integer
fromBWord (BWord x) = x
minBound' :: Integer
minBound' = fromIntegral (minBound::Word)
@mitsuji
mitsuji / BWord.hs
Created May 24, 2019 10:05
Word type supports Underflow/Overflow error
module Data.BWord(BWord(..),fromBWord) where
newtype BWord = BWord Integer
deriving (Eq,Ord,Show)
fromBWord :: BWord -> Integer
fromBWord (BWord x) = x
minBound' :: Integer
minBound' = fromIntegral (minBound::Word)
{-# LANGUAGE OverloadedStrings #-}
import Data.String (fromString)
import System.Environment (getArgs)
import qualified Network.Wai.Handler.Warp as Warp
import qualified Network.Wai as Wai
import qualified Network.HTTP.Types as H
import qualified Data.ByteString as BS -- use for input
import qualified Data.ByteString.Lazy as LBS -- use for out
type MemberId = Int
type ItemId = Int
data Sex = Man | Woman deriving (Show,Eq)
data Member = Member {
memberId :: MemberId
,memberName :: String
,memberSex :: Sex
@mitsuji
mitsuji / either.hs
Last active January 10, 2016 01:56
import Data.Either (isLeft)
type Err = String
type ID = Int
type Rank = Int
type Name = String
type RankDB = [(Rank,ID)]
type NameDB = [(ID,Name)]
idFromRank :: RankDB -> Rank -> Either Err ID
@mitsuji
mitsuji / maybe.hs
Last active January 10, 2016 01:43
import Data.Maybe (isNothing,fromJust)
type ID = Int
type Rank = Int
type Name = String
type RankDB = [(Rank,ID)]
type NameDB = [(ID,Name)]
idFromRank :: RankDB -> Rank -> Maybe ID
idFromRank db rk = lookup rk db
import Data.List (foldl')
import Control.Monad.Reader (Reader,runReader,ask)
import Control.Monad (foldM)
main = do
print $ total 0.08 [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]
print $ totalG [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]
print $ totalR 0.08 [(108,True,2),(200,False,1),(324,True,2),(400,False,1)]
@mitsuji
mitsuji / bc.hs
Last active August 29, 2015 14:27
broadcast by STM
import Control.Concurrent(forkIO,threadDelay)
import qualified Control.Concurrent.STM.TChan as TChan
import qualified Control.Monad.STM as STM
main = do
rp <- TChan.newTChanIO
bc <- TChan.newBroadcastTChanIO
forkIO $ broadcast bc
forkIO $ receive 0 rp bc
forkIO $ receive 1 rp bc
@mitsuji
mitsuji / pc.hs
Created August 19, 2015 09:13
Producer-Consumer by STM
import Control.Concurrent(forkIO,threadDelay)
import qualified Control.Concurrent.STM.TChan as TChan
import qualified Control.Monad.STM as STM
main = do
rp <- TChan.newTChanIO
pc <- TChan.newTChanIO
forkIO $ produce pc
forkIO $ produce pc
forkIO $ produce pc