Skip to content

Instantly share code, notes, and snippets.

View MiyamonY's full-sized avatar
🏠
Working from home

MiyamonY

🏠
Working from home
View GitHub Profile
@MiyamonY
MiyamonY / Parser.hs
Created March 11, 2015 19:46
haskell parser combinator
import Data.Char (isDigit, isLower, isUpper, isAlpha, isAlphaNum, isSpace)
import Test.HUnit
import Test.HUnit.Tools (assertRaises)
import Control.Exception (ErrorCall(ErrorCall), evaluate)
newtype Parser a = Parser {getParser :: String -> [(a, String)]}
parse :: Parser a -> String -> [(a, String)]
parse p inp = (getParser p) inp
@MiyamonY
MiyamonY / StrConverter.hs
Created March 7, 2015 08:17
haskell StrConverter
import Data.Char (ord, chr)
type Bit = Int
bin2int :: [Bit] -> Int
bin2int bits = sum [w * b | (w, b) <- zip weights bits]
where weights = iterate (* 2) 1
int2bin :: Int -> [Bit]
int2bin 0 = []
@MiyamonY
MiyamonY / MakingMonad.hs
Created March 6, 2015 16:46
haskell making monad
import Test.HUnit
import Control.Monad
import Data.Ratio
import Data.List (all)
newtype Prob a = Prob {getProb :: [(a, Rational)]} deriving (Show, Eq)
instance Functor Prob where
fmap f (Prob xs) = Prob $ map (\(x, p) -> (f x, p)) xs
@MiyamonY
MiyamonY / porland.hs
Created March 6, 2015 09:28
haskell monad porland
import Control.Monad
import Data.List
import Test.HUnit
readMaybe :: (Read a) => String -> Maybe a
readMaybe st = case reads st of [(x, "")] -> Just x
_ -> Nothing
foldingFunction :: [Double] -> String -> Maybe [Double]
foldingFunction (x:y:ys) "*" = return $ (y * x) : ys
@MiyamonY
MiyamonY / monadic.hs
Created March 6, 2015 04:26
haskell monadic function
import Test.HUnit
import Control.Monad
import Control.Monad.Writer
import qualified Control.Monad.State as S
import StateStack
import Control.Applicative
tests = ["liftM1" ~: (liftM (*3) $ Just 8) ~?= Just 24,
"liftM2" ~: (return 8 >>= \ x -> Just $ (*3) x) ~?= Just 24,
@MiyamonY
MiyamonY / error.hs
Created March 5, 2015 09:13
haskell error
import Test.HUnit
import Control.Monad.Except
-- instance (Error e) => Monad (Either e) where
-- return x = Right x
-- Right x >>= f = f x
-- Left err >>= f = Left err
-- fail msg = Left (strMsg msg)
tests = ["Either1" ~: (Left "boom" >>= (\ x -> return . succ $ x)) ~?= (Left "boom" :: Either String Int),
@MiyamonY
MiyamonY / state.hs
Created March 5, 2015 08:49
haskell state monad
import Test.HUnit
import System.Random
import qualified Control.Monad.State as S
threeCoins :: StdGen -> (Bool, Bool, Bool)
threeCoins gen =
let (firstCoin, newGen) = random gen
(secondCoin, newGen1) = random newGen
(thirdCoin, _) = random newGen1
in
@MiyamonY
MiyamonY / monad2.hs
Created March 5, 2015 08:10
haskell writer
import Test.HUnit
import System.IO
import Control.Monad.Writer
import Data.Monoid
import Control.Monad.Instances
-- isBigGang :: Int -> Bool
-- isBigGang x = x > 9
isBigGang :: Int -> (Bool, String)
import Monad
import Control.Monad
foo :: Maybe String
foo = Just 3 >>= (\ x -> Just "!" >>= \ y -> Just $ show x ++ y)
bar :: Maybe String
bar = do
x <- Just 3
y <- Just "!"
import qualified Data.Foldable as F
import Data.Monoid
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
instance Functor Tree where
fmap f EmptyTree = EmptyTree
fmap f (Node x l r) = Node (f x) (fmap f l) (fmap f r)
instance F.Foldable Tree where