📘
HPM Education - Haskell
  • Introduction to Haskell
  • Introduction
    • Functions
    • Functional Programming vs Imperative Programming
    • Installing Haskell
    • Haskell Modules
    • Loading Modules into GHCi
    • Expressions
    • Laziness
    • Immutability
  • Types in Haskell
    • Introduction
    • Basic Types
    • Static Type Check
    • Polymorphic and Overloaded Types
    • Data Structure Types
      • Lists
        • List Functions
      • Tuples
    • Function Types
      • Curried Functions
      • Partial Application
  • Defining Functions / Working with Functions
    • The Layout Rule
    • Local Definitions
    • The Infix Operator
    • Conditionals
      • If-then-else Statements
      • MultiWayIf
      • Guarded Equations
      • Case-of Statements
    • Pattern Matching
      • Tuple Patterns
      • List Patterns
    • Lambda functions
    • Function Operators
  • List Comprehensions
    • List Comprehensions
  • Higher-order Functions
    • Introduction
    • The map Function
    • The filter Function
  • Recursion
    • Introduction
    • 4 Steps to Defining Recursive Functions
    • Recursion Practice
    • Folds
      • Fold Right (foldr)
      • Fold Left (foldl)
  • Cutom Types
    • Declaring Types
      • Type Synonyms
      • Data Declarations
      • Newtype declarations
  • Type Classes
    • Introduction
    • Basic Classes
      • Eq – Equality Types
      • Ord – ordered types
      • Show – Showable Types
      • Read – readable types
      • Num – Numeric Types
      • Integral – Integral Types
      • Fractional – Fractional Types
      • Enum – Enumeration Types
    • Derived Instances
    • Exercise – Making a Card Deck Type
  • Interactive Programming
    • Introduction
    • Input / Output Actions
    • Sequencing Actions
    • Exercise - Numbers Guessing Game
  • Functors, Applicatives and Monads
    • Introduction
    • Functors
    • Applicative Functors
    • Monads
      • Maybe Monad
      • List Monad
      • Monad Laws
  • References / Further Reading
Powered by GitBook
On this page

Was this helpful?

  1. Interactive Programming

Input / Output Actions

Haskell uses a special type IO to distinguish impure, input/output actions from pure expressions. The idea of the IO type is that apart from returning some value, it may also interact with the outside world along the way. The IO type has the following structure:

IO a

where IO is the type name, and a is the parameterised value that it returns. For example:

IO Int -- an action that returns an Int
IO () -- an action that returns an empty tuple, called a unit

The last example IO () represents an action that is run solely for its side effects and simply returns an empty tuple (a void value or unit). Some basic actions in Haskell are:

getChar :: IO Char       -- reads and returns a character from the screen
putChar :: Char -> IO () -- prints a character to the screen
return  :: a -> IO a     -- returns a value as an action

Note that, strictly speaking, putChar and return are not actions, but functions that return actions. The return function is simply our one-way bridge from the pure world to the impure world that we use when we want to use pure values in actions, which we will see in examples soon. For now, let's just try out the basic actions getChar and putChar in GHCi:

ghci> getChar
1'1'            -- input is 1

ghci> getChar
'\n'


ghci> putChar 'a'
a

ghci> putChar '\n'

PreviousIntroductionNextSequencing Actions

Last updated 2 years ago

Was this helpful?