Haskell - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Haskell

Description:

More Input and More Output. Files and Streams. Transforming Input. Not covered. brackets. command-line arguments. bytestrings – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 10
Provided by: minesEdu
Category:
Tags: command | haskell | lines | unix

less

Transcript and Presenter's Notes

Title: Haskell


1
Haskell
  • Chapter 9

2
More Input and More Output
  • Files and Streams
  • Transforming Input
  • Not covered
  • brackets
  • command-line arguments
  • bytestrings

3
Transforming input
  • Common pattern get string from input, transform,
    output result
  • Use interact
  • main interact shortLinesOnly
  • shortLinesOnly String -gt String
  • shortLinesOnly unlines . filter (\line -gt
    length line lt 10) . lines

Preludegt lines "aa\nbb\nbb" "aa","bb","bb"
4
Some details
  • interact String -gt String takes a string
    containing all the input and returns a string
    containing all the output.
  • This is why we use lines/unlines with interact
  • The reason you see output after pressing enter
    with interact (map toUpper) is because map
    toUpper acts lazily -- it can start giving output
    before all the input is known.
  • In our example the output is from filter but
    same idea

http//stackoverflow.com/questions/16799755/haskel
l-interact-function
5
Another example
  • respondPalindromes String -gt String
  • respondPalindromes
  • unlines .
  • map (\xs -gt if isPal xs then "palindrome" else
    "not a palindrome") .
  • lines
  • isPal String -gt Bool
  • isPal xs xs reverse xs
  • main2 interact respondPalindromes

6
Reading a file
  • import System.IO
  • main3 do
  • handle lt- openFile "haiku.txt" ReadMode
  • contents lt- hGetContents handle
  • putStr contents
  • putStr "\n"
  • hClose handle
  • openFile FilePath -gt IOMode -gt IO Handle
  • type FilePath String
  • data IOMode ReadMode WriteMode AppendMode
    ReadWriteMode

7
Reading a file simpler
  • Haiku (from wikipedia)
  • Japanese poetry
  • Essence is cutting
  • Traditional has 17 on (syllables) in 3 phrases,
    with 5 7 5 syllables/phrase
  • import System.IO
  • main4 do
  • contents lt- readFile "haiku.txt"
  • putStr contents
  • putStr "\n"
  • readFile FilePath -gt String -gt IO()

8
Randomness
  • Referential transparency function given the same
    parameters twice must return same result
  • SO, we bring in randomness from outside (kind of
    like using Unix time stamp for a seed)
  • Take a random generator, return a random value
    and new random generator
  • random (RandomGen g, Random a) gt g -gt (a, g)
  • Take an integer, return a random generator
  • mkStdGen Int -gt StdGen

more random functions in book
9
Example
  • import System.Random
  • threeCoins StdGen -gt (Bool, Bool, Bool)
  • threeCoins gen
  • let
  • (firstCoin, newGen) random gen
  • (secondCoin, newGen') random newGen
  • (thirdCoin, newGen'') random newGen'
  • in (firstCoin, secondCoin, thirdCoin)
  • --threeCoins (mkStdGen 22)
Write a Comment
User Comments (0)
About PowerShow.com