Скачать презентацию Связь с внешним миром Input Output Лекция 8 Скачать презентацию Связь с внешним миром Input Output Лекция 8

Lecture 8.OutWorld.ppt

  • Количество слайдов: 24

Связь с внешним миром. Input/Output Лекция № 8 Связь с внешним миром. Input/Output Лекция № 8

Simple I/O examples main = do put. Str. Ln Simple I/O examples main = do put. Str. Ln "Greetings! What is your name? " inp. Str <- get. Line put. Str. Ln $ "Welcome to Haskell, " ++ inp. Str ++ "!"

Simple I/O example. Output *Main> main Greetings! What is your name? Igor Welcome to Simple I/O example. Output *Main> main Greetings! What is your name? Igor Welcome to Haskell, Igor!

Lets be more hard! name 2 reply : : String -> String name 2 Lets be more hard! name 2 reply : : String -> String name 2 reply name = "Pleased to meet you, " ++ name ++ ". n" ++ "Your name contains " ++ charcount ++ " characters. " where charcount = show (length name) main : : IO () main = do put. Str. Ln "Greetings once again. What is your name? " inp. Str <- get. Line let out. Str = name 2 reply inp. Str put. Str. Ln out. Str

Output of previous source *Main> main Greetings once again. What is your name? Igor Output of previous source *Main> main Greetings once again. What is your name? Igor Pleased to meet you, Igor. Your name contains 4 characters. *Main>

Мыть ли нам руки? Pure Impure Всегда возвращает одинаковый результат при одинаковых параметрах на Мыть ли нам руки? Pure Impure Всегда возвращает одинаковый результат при одинаковых параметрах на вход Может вернуть разные значения при одинаковых параметрах на вход Всегда исключает побочные эффекты Может иметь побочные эффекты Никогда не изменяет состояний Может изменить глобальное состояние программы, системы или мира

Работа с файлами import System. IO import Data. Char(to. Upper) main : : IO Работа с файлами import System. IO import Data. Char(to. Upper) main : : IO () main = do inh <- open. File "input. txt" Read. Mode outh <- open. File "output. txt" Write. Mode mainloop inh outh h. Close inh h. Close outh

Работа с файлами mainloop : : Handle -> IO () mainloop inh outh = Работа с файлами mainloop : : Handle -> IO () mainloop inh outh = do ineof <- h. Is. EOF inh if ineof then return () else do inp. Str <- h. Get. Line inh h. Put. Str. Ln outh (map to. Upper inp. Str) mainloop inh outh

Режимы работы с файлами IOMode Can read? Can write? Starting position Notes Read. Mode Режимы работы с файлами IOMode Can read? Can write? Starting position Notes Read. Mode Yes No Beginning of file File must exist already Write. Mode No Yes Beginning of file File is truncated (completely emptied) if it already existed Read. Write. Mode Yes Beginning of file File is created if it didn't exist; otherwise, existing data is left intact Append. Mode No Yes End of file File is created if it didn't exist; otherwise, existing data is left intact.

Where and pattern matching calc. Bmis : : (Real. Float a) => [(a, a)] Where and pattern matching calc. Bmis : : (Real. Float a) => [(a, a)] -> [a] calc. Bmis xs = [bmi w h | (w, h) <- xs] where bmi weight height = weight / height ^ 2

Let it be! Let it be!

Let it be let <bindings> in <expression> Let it be let in

Let it be cylinder : : (Real. Float a) => a -> a cylinder Let it be cylinder : : (Real. Float a) => a -> a cylinder r h = let side. Area = 2 * pi * r * h top. Area = pi * r ^2 in side. Area + 2 * top. Area

Let it be ghci> [if 5 > 3 then Let it be ghci> [if 5 > 3 then "Woo" else "Boo", if 'a' > 'b' then "Foo" else "Bar"] ["Woo", "Bar"] ghci> 4 * (if 10 > 5 then 10 else 0) + 2 42

Let it be ghci> 4 * (let a = 9 in a + 1) Let it be ghci> 4 * (let a = 9 in a + 1) + 2 42 ghci> [let square x = x * x in (square 5, square 3, square 2)] [(25, 9, 4)]

Let it be ghci> (let a = 100; b = 200; c = 300 Let it be ghci> (let a = 100; b = 200; c = 300 in a*b*c, let foo="Hey "; bar = "there!" in foo ++ bar) (6000000, "Hey there!")

Let it be with pattern matching ghci> (let (a, b, c) = (1, 2, Let it be with pattern matching ghci> (let (a, b, c) = (1, 2, 3) in a+b+c) * 100 600

Let it be with pattern matching calc. Bmis : : (Real. Float a) => Let it be with pattern matching calc. Bmis : : (Real. Float a) => [(a, a)] -> [a] calc. Bmis xs = [bmi | (w, h) <- xs, let bmi = w / h ^ 2]

Let it be with pattern matching calc. Bmis : : (Real. Float a) => Let it be with pattern matching calc. Bmis : : (Real. Float a) => [(a, a)] -> [a] calc. Bmis xs = [bmi | (w, h) < xs, let bmi = w / h ^ 2, bmi >= 25. 0]

Let it be in interaction mode ghci> let zoot x y z = x Let it be in interaction mode ghci> let zoot x y z = x * y + z ghci> zoot 3 9 2 29 ghci> let boot x y z = x * y + z in boot 3 4 2 14 ghci> boot : 1: 0: Not in scope: `boot'

Case expressions case expression of pattern -> result . . . Case expressions case expression of pattern -> result . . .

Case expressions describe. List : : [a] -> String describe. List xs = Case expressions describe. List : : [a] -> String describe. List xs = "The list is " ++ case xs of [] -> "empty. " [x] -> "a singleton list. " xs -> "a longer list. "

Case expressions describe. List : : [a] -> String describe. List xs = Case expressions describe. List : : [a] -> String describe. List xs = "The list is " ++ what xs where what [] = "empty. " what [x] = "a singleton list. " what xs = "a longer list. "

Over Over