A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Haskell The Craft of Functional Programming Simom Thompson Capítulo 14

Apresentações semelhantes


Apresentação em tema: "Haskell The Craft of Functional Programming Simom Thompson Capítulo 14"— Transcrição da apresentação:

1 Haskell The Craft of Functional Programming Simom Thompson Capítulo 14
Input/Output Haskell The Craft of Functional Programming Simom Thompson Capítulo 14

2 Os tipos IO t getLine :: IO String putStr :: String -> IO ()
Os objetos que pertencem ao tipo IO t irão realizar algum IO (entrada/saída) e retornarão um valor do tipo t. getLine :: IO String putStr :: String -> IO () putStrLn :: String -> IO () Ler uma linha da entrada. Escreve uma String na tela. O resultado desta interação tem o tipo (). É usado quando algum valor variado deve ser retornado, tal valor não tem importância para outras funções. É um caso especial da função putStr: putStrLn st = putStr (st ++ “\n”)

3 A operação ‘then’ (>>=) :: IO t -> (t -> IO u) -> IO u
A operação ‘then’ simbolizada por >>= dá seqüência a duas operações, uma após a outra. (>>=) :: IO t -> (t -> IO u) -> IO u Esta operação combina um IO t com uma função que pega o resultado disso (tipo t) e retorna uma IO u. IO t t u IO Nós podemos junta-las, passando o resultado da primeira como primeiro argumento da segunda. IO t u IO u

4 Exemplo main :: IO() main = putStr "Digite seu nome:" >>
getLine >>= \ st -> putStr "O seu nome ao contrario e':" >> putStr (reverse st)

5 Outras Iterações return :: t -> IO t
(>>) :: IO t -> IO u -> IO u apply :: (t -> u) -> t -> IO u Faz uma iteração trivial: simplesmente retorna uma valor sem algum I/O. Caso não queria passar o resultado da primeira para a segunda iteração. f >> g = f >>= const g Permite mudar o valor o resultado de um objeto IO. apply f a = return (f a)

6 As funções de iteração do tipo IO t
getLine :: IO String putStr :: String -> IO () putStrLn :: String -> IO () (>>=) :: IO t -> (t -> IO u) -> IO u] (>>) :: IO t -> -> IO u -> IO u return :: t -> IO t apply :: (t -> u) -> t -> IO u while :: IO Bool -> IO () -> IO () interact :: (String -> String) -> IO ()

7 Exemplo de Projeto doisJog :: Window -> Tabuleiro -> Vez -> IO () doisJog w tb v = clearWindow w >> draw w (overMany ((quadrados w tb 120) ++ divisoes ++ tabuleiro ++ botaoSair ++ botaoNovo ++ background)) >> pintaVez w v >> casaEscolhida w tb >>= \ casa -> verClique casa >>= \ click -> case click of "S" -> closeWindow w "N" -> main

8 Continuação _ -> if (coordValida casa tb) then
marca w casa v >> colocaPeca1 casa tb v >>= \ newtab -> if (perdeu v newtab) then if v==Vermelha then fim w "O vermelho ganhou!" else fim w "O cinza ganhou!" doisJog w newtab (trocaVez v) doisJog w tb v

9 Arquivos readFile :: (arquivo) -> String
Leitura de arquivos: readFile :: (arquivo) -> String Escrever em arquivos: writeFile :: (arquivo) -> String ->

10 Exemplo main :: IO () main = putStrLn ”Escrevendo no arquivo teste.txt" >> writeFile "teste.txt" (listIntToString [4,5,11,23]) >> putStrLn ”Lendo o arquivo teste.txt" >> readFile "teste.txt" >>= \x -> x e’ uma string! putStrLn x >> putStrLn ”Soma da lista do arquivo teste.txt e’:" >> putStrLn (show (sum (stringToListInt x))) listIntToString :: [Int] -> String listIntToString [] = "F" fim! listIntToString (x:xs) = show x ++ " " ++ listIntToString xs para inteiros poder usar

11 Continuação... showstringToListInt :: String -> [Int]
stringToListInt "F" = [] stringToListInt xs = [(stringToInt 0 (takeWhile (/= ' ') xs))] ++(stringToListInt (tail (dropWhile (/= ' ') xs))) -- o tail acima e' para tirar o espaco em branco entre os inteiros.-- senao o programa entra em loop! stringToInt :: Int -> String -> Int stringToInt accum [] = accumstringToInt accum (x:xs) = stringToInt (accum*10 + ord x - ord '0') xs


Carregar ppt "Haskell The Craft of Functional Programming Simom Thompson Capítulo 14"

Apresentações semelhantes


Anúncios Google