Haskell IO 1 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Haskell IO 1

Description:

Int, Float, Char, Bool, Integer, Rational. Lists and Tuples ... equal to b, the function will then countdown from a to b, outputting all these numbers. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 33
Provided by: csWaik
Category:

less

Transcript and Presenter's Notes

Title: Haskell IO 1


1
Haskell IO 1
2
Haskell Review
  • Environment uses commands prefixed by colon
  • ? -- for help
  • load to load up a file
  • quit
  • edit
  • Types
  • Int, Float, Char, Bool, Integer, Rational
  • Lists and Tuples
  • 1,2,3 all elements of the same type
  • 1..5 gives consecutive numbers
  • Strings
  • String Char
  • 12,3 and 1,2,3 4,5,6

3
Defining your own functions
  • sq Int -gt Int -- First you define the type
  • sq x x x -- Then the definition of the
    function
  • mystrlen String -gt Int
  • mystrlen 0
  • mystrlen (ht) 1 mystrlen t
  • -- If you dont want to worry about the type!
  • mystrlen 0
  • mystrlen (ht) 1 mystrlen t
  • To find the type simply type
  • gt type function, eg
  • gt type mystrlen

4
Review Continued
  • Problem 1 write a Haskell function to perform
    palindrome checks, for example
  • gtpalindrome abc
  • False
  • gtpalindrome abba
  • True
  • gtpalindrome abba1
  • False
  • First write a function mylast that returns the
    last character in a string.

5
Palindrome code
  • mylast String -gt Char
  • mylast (x ) x
  • mylast (ht) mylast t
  • palindrome String -gt Bool
  • palindrome True
  • palindrome (h ) False
  • palindrome (ht) (h mylast t)
  • palindrome (ht) palindrome t

6
More on Functions
  • Functions can be written to work with options
  • hcf Int -gt Int -gt Int
  • hcf n m
  • m n n
  • m gt n hcf m n
  • otherwise hcf (n-m) m
  • gt hcf 24 32
  • 8

7
Rewrite Palindrome with guards
  • palindrome1 String -gt Bool
  • palindrome1 a
  • a True
  • tail a False
  • (head a mylast (tail a)) True
  • otherwise palindrome1 (tail a)

8
Useful String Operations
  • gt lines ABC\nDEF\nGHI
  • ABC, DEF, GHI
  • gt unlines "ABC", "DEF", "GHI"
  • "ABC\nDEF\nGHI\n"
  • gt words " The quick \n brown fox.."
  • "The", "quick", "brown", "fox.."
  • gt unwords "The","quick","brown","fox.."
  • "The quick brown fox..
  • gt show 345
  • gt 345 -- so show converts anything to a
    string

9
Word Count Example
  • wc String -gt (Int,Int)
  • wc s (length(lines s), length(words s))
  • prettywc s "Lines " show l
  • "\nWords " show w "\n"
  • where (l,w) wc s
  • Note that where uses indentation!
  • gt wc "This is\na test"
  • (2,4)
  • gt prettywc "This is\na b c d e f g h"
  • Lines 1\nWords 10\n

10
Alternative to indentation
  • getw (Int,Int) -gt Int
  • getw (l,w) w
  • getl (Int,Int) -gt Int
  • getl (l,w) l
  • prettywc1 s "Lines " show (getl(wc s))
  • "\nWords " show (getw(wc s))
    "\n"

11
Output options
  • Still got the show problems
  • gt putStr(prettywc This is a\ntest)
  • Lines 1
  • Words 4
  • putStr is an IO function it has an IO type!
  • gt type putStr
  • putStr String -gt IO ( )

12
Haskell IO
  • Haskell IO is purely functional
  • Monad is from "Category Theory" (Maths)
  • Basically, we need to thread a state argument
    through all function calls, for which each call
    also has to return the new state.
  • In Haskell/Hugs, each I/O action returns a value
    so has a type
  • IO a (where a is the return type).
  • Eg getLine IO String
  • getChar IO Char

13
Haskell IO
  • Some actions have no interesting values, like
    putStr and
  • putChar Char -gt IO ( )
  • This is similar to a void type.

14
Output Actions
  • These write to standard output (cout)
  • putChar Char -gt IO ()
  • putStr String -gt IO ()
  • putStrLn String -gt IO () -- adds \n
  • These write to (or create) output files
  • type FilePath String
  • writeFile FilePath -gt String -gt IO()
  • appendFile FilePath -gt String -gt IO()
  • Eg gt putStr "Hello World!"
  • Hello World!

15
Input Actions
  • These read from standard input (cin)
  • getChar IO Char -- gets 1 char
  • getLine IO String -- strips \n
  • getContents IO String
  • -- returns ALL the rest of cin!
  • This reads the entire contents of a file!
  • readFile FilePath -gt IO String

16
Combining Actions
  • The "do" notation is the nicest way of combining
    I/O actions into a program (209IO1.txt)
  • main
  • do putStr "Input file? "
  • ifile lt- getLine
  • putStr "Output file? "
  • ofile lt- getLine
  • s lt- readFile ifile
  • writeFile ofile (s s)
  • Note that main is a special function of type IO
    ( )

17
Syntax for the do statement
  • do keyword, then several "statements".
  • Each statement must be one of
  • ioaction (where ioaction IO ( )
    )
  • patt lt- ioaction (where ioaction IO b)
  • (variables in patt are created, and
    initialized
  • from the b value returned by ioaction)
  • let patt expr (for local definitions)
  • if pred then ioaction else ioaction
  • return expr (just like in C!)

18
Examples
  • There are many options, below is the simplest
    style
  • getTwoChars IO (Char,Char)
  • getTwoChars do
  • c1 lt- getChar
  • c2 lt- getChar
  • return (c1,c2)
  • The return is vital here because do requires an
    IO action as result. The return allows a value to
    become an IO action.
  • return a -gt IO a

19
Testing IO functions
  • gt do (c1, c2) lt- getTwoChars putChar c1
    putChar c2
  • To convert from String to a type
  • read Read a gt String -gt a
  • Print is another name for show!
  • print Show a gt a -gt IO ()
  • getInt IO Int
  • getInt do
  • line lt- getLine
  • return (read line Int)
  • gt do x lt- getInt print x

20
More useful IO functions
  • getLine1 IO String
  • getLine1 do
  • c lt- getChar
  • if c '\n' then return ""
  • else do
  • cs lt- getLine1
  • return (ccs)
  • gt do line lt- getLine1 print line

21
Getting ints with prompt
  • getIntPrompt p do
  • putStr p
  • line lt- getLine
  • return (read line Int)
  • gt do x lt- getIntPrompt Give me an intgt
    print x
  • Give me an intgt 345
  • 345

22
Getting lots of ints
  • getInts do
  • n lt- getIntPrompt Enter Int (0 to stop)
  • if n 0 then return
  • else do
  • rest lt- getInts
  • return (nrest)

23
Summing ints
  • sumInts do
  • ints lt- getInts
  • return (sum ints)
  • gt do x lt- sumInts print x
  • Enter Int (0 to stop) 1
  • Enter Int (0 to stop) 2
  • Enter Int (0 to stop) 3
  • Enter Int (0 to stop) 0
  • 6

24
IO Problem 1 (iotest.txt)
  • Write a Haskell function iotest that prompts
    for the user for two integers a and b. If a
    is larger than or equal to b, the function will
    then countdown from a to b, outputting all these
    numbers. Should a be smaller than b, then the
    function should count up from a to b, and output
    all numbers too.
  • gt iotest gtiotest gtiotest
  • Input a 9 Input a 2 Input a 4
  • Input b 7 Input b 5 Input b 4
  • 9 2 4
  • 8 3
  • 7 4
  • 5

25
Solution
  • iotest do
  • a lt- getIntPrompt "Input a "
  • b lt- getIntPrompt "Input b "
  • iotest1 a b
  • iotest1 a b
  • a lt b output_upto a b
  • otherwise output_downto a b

26
Solution Continued
  • output_upto a b
  • a gt b return ()
  • otherwise do print a output_upto (a1) b
  • output_downto a b
  • a lt b return ()
  • otherwise do print a output_downto (a-1)
    b
  • getIntPrompt p do
  • putStr p
  • line lt- getLine
  • return (read line Int)

27
IO Problem 2 (iotest2.txt)
  • Write a Haskell function that acts like a
    simple-minded desktop calculator.
  • Your program should handle addition, subtraction,
    multiplication, and division. Always prompt the
    user for an operation first. User input should be
    a single character indicating the operation
    wanted (, -, , /) or q to quit the
    calculator.
  • gt dc
  • Op(,-,,/,q) lt is user inputgt
  • Arg1 12 lt12 is user inputgt
  • Arg2 9 lt9 is user inputgt
  • Result 21

28
Solution
  • dc IO ()
  • dc
  • do
  • putStr "Op (,-,,/,q) "
  • c lt- getChar
  • if c 'q' then return ()
  • else do
  • perform c
  • dc

29
Solution Continued
  • getInt IO Int
  • getInt do
  • line lt- getLine
  • return (read line Int)
  • getFloat IO Float
  • getFloat do
  • line lt- getLine
  • return (read line Float)

30
Calculator Operations
  • perform Char -gt IO()
  • perform '' do (arg1,arg2) lt- prompt putStr
    ("Result " show(arg1arg2) "\n")
  • perform '-' do (arg1,arg2) lt- prompt putStr
    ("Result " show(arg1-arg2) "\n")
  • perform '' do (arg1,arg2) lt- prompt putStr
    ("Result " show(arg1arg2) "\n")
  • perform '/' do (arg1,arg2) lt- promptf
    process (arg1,arg2)

31
Still more
  • prompt IO (Int,Int)
  • prompt do
  • putStr "\nArg1 "
  • arg1 lt- getInt
  • putStr "Arg2 "
  • arg2 lt- getInt
  • return (arg1,arg2)

32
Finally
  • promptf IO (Float,Float)
  • promptf do
  • putStr "\nArg1 "
  • arg1 lt- getFloat
  • putStr "Arg2 "
  • arg2 lt- getFloat
  • return (arg1,arg2)
  • process (arg1, arg2)
  • arg2 0 putStr "ERROR Attempt to divide
    by zero\n"
  • otherwise putStr ("Result "
    show(arg1/arg2) "\n")
Write a Comment
User Comments (0)
About PowerShow.com