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 ExistentialQuantification #-} | |
import Control.Applicative as A | |
import Data.Foldable as F | |
data Fold a b = forall x . Fold (x -> a -> x) x (x -> b) | |
instance Functor (Fold a) where | |
f `fmap` Fold step zero map = Fold step zero (f . map) |
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
Prelude> data GAST a = Literal String | If a a a deriving (Eq, Show) | |
Prelude> newtype AST = AST (GAST AST) deriving (Eq, Show) | |
Prelude> data Annotated a = Annotated a deriving (Eq, Show) | |
Prelude> newtype AAST = AAST (Annotated (GAST AAST)) deriving (Eq, Show) | |
Prelude> let x = AST $ If (AST $ Literal "foo") (AST $ Literal "bar") (AST $ Literal "baz") | |
Prelude> let y = AAST $ Correct $ If (AAST $ Wrong $ Literal "foo") (AAST $ Correct $ Literal "bar") (AAST $ Wrong $ Literal "baz") | |
Prelude> :t x | |
x :: AST | |
Prelude> :t y | |
y :: AAST |
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 System.Console.Haskeline | |
import Control.Monad | |
import Control.Monad.Trans.Class | |
import Control.Monad.Trans.Maybe | |
main :: IO () | |
main = runInputT defaultSettings repl | |
repl :: InputT IO () | |
repl = fmap (const ()) $ runMaybeT $ forever $ do |
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
-- This is an exercise solution from the book Beginning Haskell. | |
-- with -O3 running time for 100000 is 5.4 seconds | |
{-# LANGUAGE PackageImports #-} | |
import "mtl" Control.Monad.State | |
factState :: StateT Integer (State Integer) () | |
factState = do | |
n <- get | |
when (n > 1) $ do |
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
var readerLift = function(f) { | |
var m = function(r){ | |
return f(r) | |
}; | |
m.map = function(g){ | |
return readerLift(function(r){ | |
return g(f(r)) | |
}); | |
}; | |
m.flatMap = function(g){ |
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
module Percentage | |
( Percentage -- abstract | |
, mkPercentage | |
, getPercentage | |
, discount | |
) where | |
import Data.Monoid | |
newtype Percentage a = Percentage { runPercentage :: 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
class HasName a where | |
getName :: a -> String | |
class HasAge a where | |
getAge :: a -> Int | |
data Person = Person String Int deriving (Eq, Show) | |
instance HasName Person where | |
getName (Person name _) = name |
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
newtype Fix f = Fix { unFix :: f (Fix f) } | |
type Algebra f a = f a -> a | |
type InitialAlgebra f = Algebra f (Fix f) | |
cata :: Functor f => Algebra f a -> Fix f -> a | |
cata alg = go where go = alg . fmap go . unFix | |
-------------------------- |
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.IORef | |
import System.IO.Unsafe | |
unsafeCast :: a -> b | |
unsafeCast a = unsafePerformIO $ do | |
let ref = unsafePerformIO $ newIORef undefined | |
writeIORef ref a | |
readIORef ref | |
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
-- direct port, with implementation details, not so nice | |
import Control.Monad | |
import Control.Monad.Trans | |
import Control.Monad.Trans.Maybe | |
import Data.IORef | |
data Alcohol = BEER deriving Show | |
main = do |
OlderNewer