SlideShare a Scribd company logo
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 and
fetch some value from the outside world for us.” ([1])
  fetch some value from the outside world for us.” ([1])
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)
Haskell Features
●   Pure Functional Programming Language
●   Lazy Evaluation
●   Pattern Matching and Guards
●   List Comprehension
●   Type Polymorphism
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
       –   → don't care about the steps
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
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!
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]
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
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
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
The one and only program (1)
●   let's write the first IO program:
         main = putStrLn “Hello World!”
●   store it in helloworld.hs
●   compile instructions:
         ghc --make helloworld.hs
●   and execute:
         ./helloworld
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
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
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
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”
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.”
Program Loop
●   use return to stop:
      main = do
         putStrLn “Your Name:”
         name <- getLine
         if null name
         then return ()
         else do
            putStrLn $ “Hello ” ++ name
            main
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
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
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)

More Related Content

PPTX
Lua Study Share
PPTX
Functional programming in javascript
PDF
Introduction kot iin
PDF
Swift Tutorial 2
PPTX
Groovy
PDF
Pune Clojure Course Outline
PPTX
Javascript
PPTX
Useful JMeter functions for scripting
Lua Study Share
Functional programming in javascript
Introduction kot iin
Swift Tutorial 2
Groovy
Pune Clojure Course Outline
Javascript
Useful JMeter functions for scripting

What's hot (20)

PPTX
Functional Programming with JavaScript
PPTX
Hello kotlin | An Event by DSC Unideb
PDF
Reactive Programming in the Browser feat. Scala.js and PureScript
PDF
Hadoop + Clojure
PDF
GPars For Beginners
PDF
Building a Tagless Final DSL for WebGL
PPT
Groovy
PPT
Basic Javascript
PPTX
Java8 and Functional Programming
PPTX
Nice to meet Kotlin
PDF
Ast transformations
PDF
Zhongl scala summary
PDF
Clojure concurrency overview
PDF
CoffeeScript - JavaScript in a simple way
PDF
Intro to Java 8 Closures (Dainius Mezanskas)
PDF
Javascript foundations: Function modules
PDF
JavaScript - Chapter 6 - Basic Functions
PDF
Java 8 Stream API and RxJava Comparison
KEY
Clojure Intro
Functional Programming with JavaScript
Hello kotlin | An Event by DSC Unideb
Reactive Programming in the Browser feat. Scala.js and PureScript
Hadoop + Clojure
GPars For Beginners
Building a Tagless Final DSL for WebGL
Groovy
Basic Javascript
Java8 and Functional Programming
Nice to meet Kotlin
Ast transformations
Zhongl scala summary
Clojure concurrency overview
CoffeeScript - JavaScript in a simple way
Intro to Java 8 Closures (Dainius Mezanskas)
Javascript foundations: Function modules
JavaScript - Chapter 6 - Basic Functions
Java 8 Stream API and RxJava Comparison
Clojure Intro
Ad

Viewers also liked (6)

PDF
08. haskell Functions
PDF
04. haskell handling
PDF
01. haskell introduction
ODP
06. haskell type builder
ODP
03. haskell refresher quiz
PDF
02. haskell motivation
08. haskell Functions
04. haskell handling
01. haskell introduction
06. haskell type builder
03. haskell refresher quiz
02. haskell motivation
Ad

Similar to 05. haskell streaming io (20)

ZIP
Haskell Jumpstart
PDF
Real World Haskell: Lecture 1
PDF
DEFUN 2008 - Real World Haskell
KEY
Functional programming in clojure
KEY
Beauty and Power of Go
PDF
Purely Functional I/O
PDF
Haskell for Scala-ists
PDF
7li7w devcon5
PDF
10. haskell Modules
PPT
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
PDF
Smalltalk, the dynamic language
PDF
Functional programming using haskell notes iitk
PPTX
Tech Days Paris Intoduction F# and Collective Intelligence
PDF
Programming In Lua 3rd Edition Roberto Ierusalimschy
PDF
Clojure intro
PDF
Functions
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
PPT
Haskell retrospective
Haskell Jumpstart
Real World Haskell: Lecture 1
DEFUN 2008 - Real World Haskell
Functional programming in clojure
Beauty and Power of Go
Purely Functional I/O
Haskell for Scala-ists
7li7w devcon5
10. haskell Modules
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Smalltalk, the dynamic language
Functional programming using haskell notes iitk
Tech Days Paris Intoduction F# and Collective Intelligence
Programming In Lua 3rd Edition Roberto Ierusalimschy
Clojure intro
Functions
Столпы функционального программирования для адептов ООП, Николай Мозговой
Haskell retrospective

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Encapsulation_ Review paper, used for researhc scholars
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
The AUB Centre for AI in Media Proposal.docx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Mobile App Security Testing_ A Comprehensive Guide.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Cloud computing and distributed systems.
Teaching material agriculture food technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

05. haskell streaming io

  • 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 and fetch 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 – → don't 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) ● let's 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)