This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad | |
while :: (Monad m) => (a -> Bool) -> m a -> m a | |
while f m = do | |
a <- m | |
when (f a) $ while f m >> return () | |
return $ a | |
main = do | |
putStrLn $ "Can you guess the number I have?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
forkIO :: IO () -> IO ThreadId -- IO ()を渡すとその場で実行 | |
killThread :: ThreadId -> IO () -- 指定のThreadをkillする | |
threadDelay :: Int -> IO () -- スレッドを指定の時間[microsec]だけ停止する | |
data MVar a -- スレッド内で使えるmutableな変数 | |
newMVar :: a -> IO (MVar a) -- 変数を初期化 | |
takeMVar :: MVar a -> IO a -- 変数の値を取り出す | |
putMVar :: MVar a -> a -> IO () -- 変数に代入する |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses #-} | |
{-# LANGUAGE FlexibleContexts, UndecidableInstances #-} | |
import Data.Bifunctor | |
import Data.Biapplicative | |
import Data.List | |
data Tensor a b = a :*: b deriving (Show) | |
class ShowAll c where | |
toList :: [c] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Functor.Free | |
import Data.Monoid | |
type CSV a = Free Monoid [a] | |
cell :: a -> CSV a | |
cell = unit . return | |
fromList :: [a] -> CSV a | |
fromList = unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeOperators, MultiParamTypeClasses, FlexibleInstances #-} | |
{-# LANGUAGE FlexibleContexts, TypeFamilies, UndecidableInstances #-} | |
import Control.Comonad | |
data BigCrunch = BigCrunch deriving (Show) | |
data f :> g = f :> g deriving (Show) | |
infixr :> | |
class Universal c a where | |
runUniverse :: c -> a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sqRootPair :: Int -> [(Int, Int)] | |
sqRootPair n = filter (\(x,y) -> x^2 + y^2 == n) | |
[(x,y) | x <- [1..n], y <- [1..n], x < y, x^2 < n, y^2 < n] | |
show' :: [(Int, Int)] -> String | |
show' ns = ((\(x,y) -> show $ x^2 + y^2) $ ns !! 0) ++ " :" ++ show ns | |
main = do | |
mapM_ (putStrLn . show') $ filter (\x -> length x /= 0) [sqRootPair n | n <- [1,3..]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, DataKinds, TypeFamilies, ConstraintKinds, TypeOperators, UndecidableInstances #-} | |
import GHC.Prim (Constraint) | |
import Data.Ratio ((%)) | |
type family All (cx :: * -> Constraint) (xs :: [*]) :: Constraint | |
type instance All cx '[] = () | |
type instance All cx (x ': xs) = (cx x, All cx xs) | |
data HList (as :: [*]) where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds, GADTs, KindSignatures, FlexibleInstances, FlexibleContexts, TypeFamilies #-} | |
import Control.Applicative | |
data Nat = Zero | Succ Nat | |
type family Plus (n :: Nat) (m :: Nat) :: Nat | |
type instance Plus (Succ n) m = Succ (Plus n m) | |
type instance Plus Zero m = m | |
data LengthList n a where | |
Nil :: LengthList Zero a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, DataKinds, KindSignatures, TypeOperators, ConstraintKinds #-} | |
{-# LANGUAGE TypeFamilies, UndecidableInstances, FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
import GHC.Prim (Constraint) | |
import Data.Typeable | |
type family All (cx :: * -> Constraint) (xs :: [*]) :: Constraint | |
type instance All cx '[] = () | |
type instance All cx (x ': xs) = (cx x, All cx xs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, TemplateHaskell, FlexibleContexts #-} | |
import Control.Arrow | |
import Data.List | |
import Control.Monad.Operational.Mini | |
import Control.Monad.Operational.TH (makeSingletons) | |
import Control.Monad.State | |
data Pattern p q x where | |
GetLocal :: Pattern p q p | |
PutLocal :: p -> Pattern p q () |