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

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

Input/Output em Haskell

Apresentações semelhantes


Apresentação em tema: "Input/Output em Haskell"— Transcrição da apresentação:

1 Input/Output em Haskell

2 O tipo IO t As expressões do tipo IO t irão realizar alguma operação de entrada/saída e retornarão um valor do tipo t. getLine :: IO String Lê uma linha da entrada. putStr :: String -> IO () Escreve uma String na tela. O resultado desta interação tem tipo ( ), que é uma tupla vazia. Neste caso significa dizer que a função não retorna nenhum resultado interessante, apenas faz I/O.

3 O tipo IO t getChar :: IO Char Lê um caracter do teclado.
putStrLn :: String -> IO () É um caso especial da função putStr, que insere um enter no final.

4 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 dessa expressão (do tipo t) e retorna algo de tipo IO u. IO t t u IO Nós podemos combinar as expressões, passando o resultado da primeira como primeiro argumento da segunda. IO t u IO u

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

6 Outras Iterações return :: t -> IO t Simplesmente retorna um valor do tipo IO t (converte do tipo t para o tipo IO t). (>>) :: IO t -> IO u -> IO u igual ao >>=, mas ignora o resultado da primeira para a segunda interação.

7 As funções de iteração do tipo IO t
getLine :: IO String getChar :: IO Char putStr :: String -> IO () putStrLn :: String -> IO () (>>=) :: IO t -> (t -> IO u) -> IO u] (>>) :: IO t -> -> IO u -> IO u return :: t -> IO t

8 Arquivos type NomeArquivo = String
Leitura de arquivos: type NomeArquivo = String readFile :: NomeArquivo -> IO String Escrever em arquivos: writeFile :: NomeArquivo -> String -> IO ()

9 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

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

11 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)) >> mostraVez w v >> casaEscolhida w tb >>= \ casa -> verClique casa >>= \ click -> case click of "S" -> closeWindow w "N" -> main

12 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


Carregar ppt "Input/Output em Haskell"

Apresentações semelhantes


Anúncios Google