PROGRAMMING IN HASKELL Haskell

Download PROGRAMMING IN HASKELL  Haskell

Post on 01-Jan-2016

78 views

Category:

Documents

5 download

DESCRIPTION

PROGRAMMING IN HASKELL Haskell. Chapter 9 - Interactive Programs . ( 2011 ) http://www.cs.nott.ac.uk/~gmh/book.html . batch program. inputs. outputs. Introduction. - PowerPoint PPT Presentation

TRANSCRIPT

  • *PROGRAMMING IN HASKELLHaskellChapter 9 - Interactive Programs (2011) http://www.cs.nott.ac.uk/~gmh/book.html

  • *Introduction8 Haskell

  • *Haskell

  • *Haskell ::Haskell (side effect)

  • *Haskell IO a a

  • *For example:IO CharIO ()() ( p.23)Note:

  • * 3 :getChar :: IO Char getChar 1 :

  • * purChar c c :putChar :: Char IO () return v v :return :: a IO a

  • * do

    :(Sequencing)a :: IO (Char,Char)a = do x getChar getChar y getChar return (x,y)8 do

  • *getLine :: IO StringgetLine = do x getChar if x == '\n' then return [] else do xs getLine return (x:xs) 1 :

  • *putStr :: String IO ()putStr [] = return ()putStr (x:xs) = do putChar x putStr xs::putStrLn :: String IO ()putStrLn xs = do putStr xs putChar '\n'

  • *Example:strlen :: IO ()strlen = do putStr "Enter a string: " xs getLine putStr "The string has " putStr (show (length xs)) putStrLn " characters"

  • *For example:> strlen

    Enter a string: abcdeThe string has 5 characters Note:

  • *

  • *Hangman:

  • *hangman :: IO ()hangman = do putStrLn "Think of a word: " word sgetLine putStrLn "Try to guess it:" guess wordHaskell:

  • * sgetLine 1 :sgetLine :: IO StringsgetLine = do x getCh if x == '\n' then do putChar x return [] else do putChar '-' xs sgetLine return (x:xs)

  • *primitive getCh :: IO CharNote: getCh 1

    Hugs :

  • * guess guess :: String IO ()guess word = do putStr "> " xs getLine if xs == word then putStrLn "You got it!" else do putStrLn (diff word xs) guess word

  • * diff 1 2 ::> diff "haskell" "pascal" "-as--ll"diff :: String String Stringdiff xs ys = [if elem x ys then x else '-' | x xs]

  • (9)Haskell IO a a IO CharIO ()getChar :: IO Char1 putChar :: Char -> IO ()1 return v :: a -> IO a v getLine :: IO StringputStr :: String -> IO () (putStrLn )do { x p1; p2; ; y pn; } ()*

  • (9) p.109 strlen Hangman.hs Web http://www.ist.aichi-pu.ac.jp/lab/yamamoto/program_languages/2011/*

  • (Linux)http://www.cs.nott.ac.uk/~gmh/book.html Code calculator.lhs (Calculator, 9.6 )life.lhs (Game of life, 9.7 )Parsing.lhs (Functional parsing library, 8 )calculator.lhs Parsing.lhs calculator.lhs Parsing.lhs import Linux ghci calculator.lhs run life.lhs life glider *

  • (Windows, 1)http://www.cs.nott.ac.uk/~gmh/book.html Code Parsing.lhs (Functional parsing library, 8 )http://www.ist.aichi-pu.ac.jp/lab/yamamoto/program_languages/ 2011 calculatoWin.lhs (Calculator, 9.6 )lifeWin.lhs (Game of life, 9.7 )calculatorWin.lhs Parsing.lhs calculatorWin.lhs Parsing.lhs import *

  • (Windows, 2)ansi-terminal cabal Haskell Platform $ cabal updateansi-terminal $ cabal install ansi-terminalWindows GHCi WinGHCi calculatorWin.lhs run lifeWin.lhs life glider

    *

  • *

  • *Nim Haskell : 5 :1: * * * * *2: * * * *3: * * *4: * *5: *

  • *2 1 1

    :

    5 [5,4,3,2,1]