项目作者: DimaOanaTeodora

项目描述 :
FLP Labs
高级语言: Haskell
项目地址: git://github.com/DimaOanaTeodora/Haskell-Monads.git
创建时间: 2021-04-08T14:56:33Z
项目社区:https://github.com/DimaOanaTeodora/Haskell-Monads

开源协议:

下载


Haskell Course :crystal_ball:

Monads Labs

Tests + Solutions:

:arrow_down: Haskell Code :arrow_down:

:biohazard: Class 1

  • Mini-Haskell
  • Counter
  • lambda
  • operator $

:biohazard: Class 2

  • Statements
  • While
  • Operators := and :

:biohazard: Class 3

  1. >= aplica si pastreaza tipul de apel
  2. >> transforma
  3. <=< compunere inversa
  4. (Just 3) >>= (\ x -> if (x>0) then Just (x*x) else Nothing) => Just 9
  5. (Just 3) >> (Just 6) => Just 6
  6. f <=< g $ 3 => Just 10 (il ia pe 3, ii aplica g, si valorii obtinute ii aplica f)

:biohazard: Class 4-5

* Identity Monad and data Value

  1. type M = Identity
  2. newtype Identity a = Identity { runIdentity :: a }
  3. term0 = (App (Lam "x" (Var "x" :+: Var "x")) (Con 10 :+: Con 11)) => "42"
  • data Value
  • interpretor mini Haskell for Identity Monad with data Value
  • test program
  • showM function for Identity monad

    * Maybe Monad and data Value

    1. type M = Maybe
    2. term0 = (App (Lam "x" (Var "x" :+: Var "x")) (Con 10 :+: Con 11)) => Just 42
  • Maybe Monad (is system defined)
  • data Value (without Wrong)
  • interpretor mini Haskell for Maybe Monad with data Value
  • test programs
  • without showM function (system defined for Maybe)

    * Either String Monad and data Value

    1. type M = Either String
    2. term0 = (App (Lam "x" (Var "x" :+: Var "x")) (Con 10 :+: Con 11)) => Right 42
    3. term1 = (App (Con 7) (Con 2)) [] => Left "should be function: 7"
  • Either String Monad (is system defined)
  • data Value
  • interpretor mini Haskell for Either String Monad with data Value
  • test programs
  • without showM function (system defined for Either String)
  • Left “error text” or Left (show Wrong) for errors

    * List Monad and data Value

    1. -> intoarce lista raspunsurilor posibile
    2. type M a = [a]
    3. interp (App (Lam "x" (Var "x" :+: Var "x")) (Amb (Con 1) (Con 2)))) [] => [2,4]
  • List Monad (is system defined)
  • data Value
  • interpretor mini Haskell for List Monad with data Value
  • test program
  • show function (system defined for List)
  • New: Fail and Amb constructors

    * EnvReader Monad and data Value

    1. -> mediul de evaluare este vazut o stare care este citita atunci când avem nevoie de valorile variabilelor
    2. -> dar starea nu va fi modificata(imutabila)
    3. type M = EnvReader
    4. newtype EnvReader a = Reader { runEnvReader :: Environment -> a }
    5. term0 = (App (Lam "x" (Var "x" :+: Var "x")) (Con 10 :+: Con 11)) => "42"
  • Reader Monad with environment
  • interpretor function with one argument (it contains the environment)
  • ask, local - 2 auxliary functions for EnvReader
  • new function for lookup
  • showM function

    * StringWriter Monad and data Value

    1. -> afisare de rezultate intermediare
    2. type M = StringWriter
    3. newtype StringWriter a = StringWriter { runStringWriter :: (a, String) }
    4. interp (Out (Con 41) :+: Out (Con 1)) [] => "Output: 41; 1; Value: 42"
  • tell auxiliary function for output a message
  • interpretor mini Haskell for Writer Monad with data Value
  • test program
  • showM function
  • New: Out constructor and ShowM function
  • instance Show-StrinWriter

    * State Monad and data Value

    1. -> numarul de pasi necesari pentru calcularea rezultatului (numar doar la adunare si aplicare de functie)
    2. type M = InState
    3. newtype IntState a = IntState { runIntState :: Integer -> (a, Integer) }
    4. interp ((Con 1 :+: Con 2) :+: Count) [] => "Value: 4; Count: 2"
  • modify, get auxiliary functions
  • interpretor mini Haskell for State Monad with data Value
  • test program
  • showM function
  • new: Count constructor and modify(+1) for modifying the counter
    1. Cand le folosesc?
    2. modify(+1) >> se foloseste fara DO
    3. modify(+1) se foloseste in DO

:biohazard: Class 6

:biohazard: Class 7