Your SlideShare is downloading. ×
  • Like
05. haskell streaming io
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

05. haskell streaming io

  • 179 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
179
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Streaming, IO Sebastian Rettig“I/O actions are like boxes with little feet that go out and “I/O actions are like boxes with little feet that go out andfetch some value from the outside world for us.” ([1]) fetch some value from the outside world for us.” ([1])
  • 2. Functional Programming● No Variables● Functions only, eventually stored in Modules – Behavior do not change, once defined – → Function called with same parameter calculates always the same result● Function definitions (Match Cases)● Recursion (Memory)
  • 3. Haskell Features● Pure Functional Programming Language● Lazy Evaluation● Pattern Matching and Guards● List Comprehension● Type Polymorphism
  • 4. Functional vs. Imperative (1)● Imperative Program: – give the computer a series of steps to execute – → say the computer how to do something to achieve the goal● Functional Program: – define what something is – → dont care about the steps
  • 5. Functional vs. Imperative (2)● Pure function (functional): – can not change a state (because we have no variables → no state to change) – can only return some result – → call 2 times with same parameters has always to return the same result! – e.g.: add:: Tree a -> a -> Tree a ● returns a complete new tree, because function can not change the state
  • 6. Functional vs. Imperative (3)● Imperative function: – can change a state → has side-effects – no guarantee, that function can crash the whole program – → take care of all possible side-effects: ● validate input ● test, test, test!
  • 7. Nice to remember (1)● Lambda-Functions: – <param> <param> → <operation> – e.g.: ● a b -> a+b ● map (x -> x+3) [2,3,4] returns [5,6,7]
  • 8. Nice to remember (2)● where & let .. in: – additional definitions – let .. in: defines scope of usage ● let = definition ● in = scope of definition (optional) ● e.g.: add x = let a=9 in a + x – where: has scope in whole function ● e.g.: add x = a + x where a=9
  • 9. Nice to remember (3)● GHCi Commands (Interpreter): – :t ● returns the function header (type) ● e.g.: :t tail tail :: [a] -> [a] – :i ● returns the function definition (interface) ● e.g.: :i tail tail :: [a] -> [a] -- Defined in GHC.List
  • 10. Pure vs. Impure Functions● haskell use pure functions● a pure function can not change a state● but how can we communicate with that function?● → we have to use impure functions● → impure functions are for communicating with the outside world* (*) just a placeholder, real description in next session
  • 11. The one and only program (1)● lets write the first IO program: main = putStrLn “Hello World!”● store it in helloworld.hs● compile instructions: ghc --make helloworld.hs● and execute: ./helloworld
  • 12. The one and only program (2) main = putStrLn “Hello World!”● main = main entry point for IO actions● :i main main :: IO () -- Defined in Main● :i putStrLn putStrLn :: String -> IO () -- Defined in System.IO
  • 13. What if we want more IO actions? main = do putStrLn “Say me your Name!” name <- getLine putStrLn $ “Hello” ++ name● do syntax glues IO actions together● bind operator <- binds some result to a placeholder● $ operator switches to right associative
  • 14. IO Actions● an I/O action is like a box with little feet that will go out into the real world and do something there [1]● the only way to open the box and get the data inside it is to use the <- operator [1]● IO-Functions are impure functions – called 2 times with same parameters do not always return the same result● you can only handle impure data in an impure environment
  • 15. Bind Operator● so what type is bind to name? name <- getLine – :t getLine getLine :: IO String – name has the type String● ! The last action in a do-block can not be bound !● Quiz: Is this valid? name = “Hello” ++ getLine● Quiz: What is test? test <- putStrLn “Hello”
  • 16. Include Pure Functions● easy by using let: main = do putStrLn “Your First Name:” fname <- getLine putStrLn “Your Last Name:” lname <- getLine putStrLn “Your Age:” age <- getLine let name = fname ++ lname daysOld = yearsToDays putStrLn $ “You are ” ++ name ++ “ and ” ++ daysOld ++ “ Days old.”
  • 17. Program Loop● use return to stop: main = do putStrLn “Your Name:” name <- getLine if null name then return () else do putStrLn $ “Hello ” ++ name main
  • 18. File Streaming● readFile: reads contents of a file lazy● :t readFile readFile :: FilePath -> IO String● What is FilePath? – :i FilePath type FilePath = String -- Defined in GHC.IO – → FilePath is synonym for String
  • 19. File Streaming● e.g.: main = do contents <- readFile filename putStr contents● of course IO functions are also lazy: main = do contents <- readFile filename putStr $ take 5 contents – no matter how long the file is
  • 20. Sources[1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/, 2012/03/15)[2] The Hugs User-Manual ( http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15)[3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15)