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 MonadTransformers where | |
import Text.Read (readMaybe) | |
import Control.Monad (ap, liftM ) | |
import GHC.Base(returnIO) | |
-- maybe monad | |
-- short circuit semantics | |
ex1 :: Maybe Int | |
ex1 = do | |
_ <- Just 4 | |
_ <- Nothing |
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 FunctorExample where | |
import Prelude hiding (Functor) | |
-- an Option data type in haskell | |
-- this is used to avoid returning nulls | |
data Option a = Some a | None | |
-- 'a' is a type parameter | |
ex1 :: Option Int | |
ex1 = Some 7 |
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
data Maybe a = None | Some a | |
class Monad f where | |
return :: a -> f a | |
bind :: f a -> (a -> f b) -> f b | |
instance Monad Maybe where | |
return x = Some x | |
bind None f = None |
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
record Monad (F : Set₀ -> Set₀) : Set₁ where | |
field | |
return : ∀ {A : Set} -> A -> F A | |
_>>=_ : ∀ {A B : Set} -> F A -> (A -> F B) -> F B | |
-- laws | |
leftUnit : ∀ {A B : Set} | |
(a : A) | |
(f : A -> F B) | |
-> (return a) >>= f ≡ f a | |
rightUnit : ∀ {A : Set} |
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
Taking the derivative of a type gives you "the type of one hole contexts" of your original type. | |
What it gives you is a generic way to traverse your data type from the perspective of a 'hole' (akin to pointers). | |
ex) If I had a tree data type, I would get a way to write primative combinators | |
'left', 'right', 'up', 'down', etc. | |
-- binary tree with no data | |
data Tree = Leaf | Node Tree Tree |
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
data Term a b where | |
Map :: (a -> b) -> Term a b | |
Filter :: (a -> Maybe a) -> Term a (Maybe a) | |
fuseMap :: Term a b -> Term b c -> Term a c | |
fuseMap (Map f ) (Map g) = Map $ g . f | |
filterFuse :: Term a (Maybe a) -> Term a b -> Term a (Maybe b) | |
filterFuse (Filter p) (Map f ) = Map (\x -> p x >>= Just . f) |
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
-- List in Scala | |
sealed trait List[+A] | |
case object Nil extends List[Nothing] | |
case class Cons[+A](head: A, tail: List[A]) extends List[A | |
-- map in Scala | |
def map[A,B](as: List[A])(f: A => B): List[B] = as match { | |
case Nil => Nil | |
case x :: xs => f(x) :: map(xs)(f) |
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 Fib where | |
import Control.Arrow((>>>),(&&&)) | |
import Control.Comonad.Cofree | |
newtype Fix f = In { out :: (f (Fix f) ) } | |
data NatF a = Z | S a deriving Show | |
-- Peano natural numbers as the least fixed point of functor NatF | |
type Nat = Fix NatF |
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 Test where | |
import Control.Arrow((>>>)) | |
type Algebra f a = f a -> a | |
type CoAlgebra f a = a -> f a | |
newtype Fix f = In { out :: (f (Fix f)) } | |
data TreeF a r = Empty | Leaf a | Node r r |
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
package adt; | |
public class ADT { | |
// sealed is an experimental feature (requrires feature preview flag) of Java 15 | |
public sealed interface Expr | |
permits Const, Plus, Times { | |
} | |
// records in Java 15 | |
// records are Scala's Case Class (I'll admit Record is a much better and more accurate name!) |
OlderNewer