1 2 3 4
play

1 2 3 4 printChar 5 printChar 6 printChar xs = [printChar - PowerPoint PPT Presentation

1 2 3 4 printChar 5 printChar 6 printChar xs = [printChar a, printChar b] printChar 7 8 9 type IO = World -> World type IO a = World -> (a, World) b -> World -> (a, World) 10 IO a IO Char IO ()


  1. 1

  2. 2

  3. 3

  4. 4

  5. printChar … 5

  6. printChar 6

  7. printChar xs = [printChar ’a’, printChar ’b’] … printChar 7

  8. 8

  9. 9

  10. type IO = World -> World type IO a = World -> (a, World) b -> World -> (a, World) 10

  11. IO a IO Char IO () 11

  12. getChar :: IO Char putChar :: Char -> IO () return :: a -> IO a 12

  13. (>>=) :: IO a -> (a -> IO b) -> IO b (act1 >>= act2) = \world -> case act1 world of (v, world’) -> act2 v world’ (>>) :: IO a -> IO b -> IO b (act1 >> act2) = \world -> case act1 world of (v, world’) -> act2 world’ 13

  14. ● getLine :: IO String getLine = getChar >>= \x -> if x == ‘\n’ then return [] else (getLine >>= \xs -> return (x:xs)) ● putStr :: String → IO () putStr [] = return () putStr (x:xs) = putChar x >> putStr xs ● putStrLn :: String → IO () putStrLn xs = putStr xs >> putChar '\n' 14

  15. ● getLine :: IO String getLine = do x <- getChar if x == ‘\n’ then return [] else do xs <- getLine return (x:xs) ● putStr :: String → IO () putStr [] = return () putStr (x:xs) = do putChar x putStr xs ● putStrLn :: String → IO () putStrLn xs = do putStr xs putChar ‘\n’ 15

  16. strlen :: IO () strlen = putStr "Enter a string: ” >> getLine >>= \xs -> putStr "The string has ” >> putStr (show (length xs)) >> putStrLn " characters." 16

  17. strlen :: IO () strlen = do putStr “Enter a string: ” xs <- getLine putStr “The string has ” putStr (show (length xs)) putStrLn “ characters.” 17

  18. return :: a -> IO a (>>=) :: IO a -> (a -> IO b) -> IO b getChar :: IO Char putChar :: Char -> IO () openFile :: [Char] -> IOMode -> IO Handle • return <- 18

  19. main :: IO () main = getLine >>= \cs -> putLine (reverse cs) 19

  20. 20

  21. 21

  22. 22

  23. data Maybe a = Nothing | Just a f :: a -> Maybe b -- returns Just b or Nothing 23

  24. doQuery :: Query -> DB -> Maybe Record r :: Maybe Record r = case doQuery q1 db of Nothing -> Nothing Just r1 -> case doQuery (q2 r1) db of Nothing -> Nothing Just r2 -> case doQuery (q3 r2) db of Nothing -> Nothing Just r3 -> . . . 24

  25. thenMB :: Maybe a -> (a -> Maybe b) -> Maybe b mB `thenMB` f = case mB of Nothing -> Nothing Just a -> f a r :: Maybe Record r = doQuery q1 db `thenMB` \r1 -> doQuery (q2 r1) db `thenMB` \r2 -> doQuery (q3 r2) db `thenMB` . . . 25

  26. instance Monad [] where m >>= f = concatMap f m return x = [x] (>>=) :: [a] -> (a -> [b]) -> [b] concatMap :: (a -> [b]) -> [a] -> [b] 26

  27. 27

  28. return :: a -> M a (>>=) :: M a -> (a -> M b) -> M b return x >>= f == f x -- left identity m >>= return == m -- right identity (m >>= f) >>= g == m >>= (\x -> f x >>= g) -- associativity 28

  29. do { x’ <- return x; f x’ == do { f x } } 29

  30. do { x <- m; return x == do { m } } 30

  31. do { y <- do { x <- m; == do { x <- m; f x y <- f x; } g y g y } } 31

  32. class Monad m where >>= :: m a -> (a -> m b) -> m b >> :: m a -> m b -> m b return :: a -> m a m >> k = m >>= \_ -> k data Maybe a = Just a | Nothing thenMB :: Maybe a -> (a -> Maybe b) -> Maybe b instance Monad Maybe where (>>=) = thenMB return a = Just a 32

  33. class Monad m where >>= :: m a -> (a -> m b) -> m b >> :: m a -> m b -> m b return :: a -> m a m >> k = m >>= \_ -> k sequence :: Monad m => [m a] -> m [a] sequence [] = return [] sequence (c:cs) = c >>= \x -> sequence cs >>= \xs -> return (x:xs) 33

  34. 34

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend