This file contains hidden or 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.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 |
This file contains hidden or 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.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 = [] |
This file contains hidden or 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 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 |
This file contains hidden or 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 | |
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 |
This file contains hidden or 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 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, |
This file contains hidden or 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 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), |
This file contains hidden or 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 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 |
This file contains hidden or 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 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) |
This file contains hidden or 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 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 "!" |
This file contains hidden or 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 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 |