Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PLT-14 IO Monad

PLT-14 IO Monad

Avatar for kanaya

kanaya

July 28, 2025
Tweet

More Decks by kanaya

Other Decks in Education

Transcript

  1. pineapple.cc fx ≡ { ⟨1/x⟩ if x ≠ 0 ∅

    otherwise gx ≡ 2.0 × x g′  x ≡ ⟨gx⟩
  2. pineapple.cc fx ≡ { ⟨1/x⟩ if x ≠ 0 ∅

    otherwise gx ≡ 2.0 × x g′  x ≡ ⟨gx⟩ x′  ≡ (g′  ∘ f)x
  3. pineapple.cc fx ≡ { ⟨1/x⟩ if x ≠ 0 ∅

    otherwise gx ≡ 2.0 × x g′  x ≡ ⟨gx⟩ x′  ≡ ⟨x⟩ ↣ f ↣ g′ 
  4. pineapple.cc f :: Float -> Maybe Float f x =

    if x /= 0 then Just (1/x) else Nothing g :: Float -> Float g x = 2.0*x g’ :: Float -> Maybe Float g’ x = Just (g x) x’ = Just 2 >>= f >>= g’
  5. pineapple.cc x :: IO String x = getLine f ::

    String -> String f x = if length x > 3 then “Too long.” else x main = (f x) >>= print
  6. pineapple.cc x :: IO String x = getLine f ::

    String -> IO String f x = if length x > 3 then return “Too long.” else return x main = (f x) >>= print
  7. pineapple.cc x :: IO String x = getLine f ::

    IO String -> IO String f x = x >>= (\x’ -> if length x’ > 3 then return “Too long.” else return x’) main = (f x) >>= print
  8. pineapple.cc x :: IO String x = getLine f ::

    String -> String f x = if length x > 3 then “Too long.” else x f’ :: IO String -> IO String f’ = liftM f main = (f' x) >>= print ผղ
  9. pineapple.cc x :: IO String x = getLine f ::

    String -> String f x = if length x > 3 then “Too long.” else x f’ :: IO String -> IO String f’ = liftM f main = (return x) >>= f’ >>= print ผղ
  10. pineapple.cc x :: IO String x = getLine f ::

    String -> String f x = if length x > 3 then “Too long.” else x main = x >>= (\x’ -> return (f x’)) >>= print ผղ