Title: Functional Programming
1PROGRAMMING IN HASKELL ???????Haskell
Chapter 9 - Interactive Programs ???????
?????? ????? ??????(???????????2011?)????
?????? http//www.cs.nott.ac.uk/gmh/book.html
??????
2Introduction
8 ????Haskell ????????????????????????????????????
?????????????????????
3????Haskell ??????????????????????????????????????
?????????????????????????
4???
Haskell ???????????????????
- Haskell ??????????(side effect)?????
???????????????????????????????
5???
Haskell ??????????????????????????????????????????
??????
IO a
? a ????????????
6For example
????????????
IO Char
?????????? ???????????
IO ()
Note
- () ????????????(????????? p.23)
7???????
??????????? 3 ????????????????????????
- ????? getChar ????????? 1 ????????????????????????
??????????
getChar IO Char
8- ????? purChar c ???? c ?????????????????
putChar Char ? IO ()
- ????? return v ?????????????? v ???
return a ? IO a
9???????(???Sequencing)
8 ?? do ???????
?????????????????? do ???? ?????????????????????
?
a IO (Char,Char) a do x ? getChar
getChar y ? getChar return (x,y)
10?????????????
getLine IO String getLine do x ? getChar
if x '\n' then
return else do
xs ? getLine return (xxs)
11putStr String ? IO () putStr
return () putStr (xxs) do putChar x
putStr xs
putStrLn String ? IO () putStrLn xs do
putStr xs putChar '\n'
12Example
????????????????????????? ???????????????
strlen IO () strlen do putStr "Enter a
string " xs ? getLine
putStr "The string has " putStr
(show (length xs)) putStrLn "
characters"
13For example
gt strlen Enter a string abcde The string has 5
characters
Note
?????????????????????????????????????
14????????????????????????????
15Hangman
????????????????????
- ??????????????????
- ?????????????????????
- ??????????????????????????????????????????
- ??????????????
16Haskell?????????????????????????????????????????
hangman IO () hangman do putStrLn "Think
of a word " word ? sgetLine putStrLn
"Try to guess it" guess word
17????? sgetLine ?????????????????????????? 1
???????????
sgetLine IO String sgetLine do x ? getCh
if x '\n' then
do putChar x return
else do putChar '-'
xs ? sgetLine
return (xxs)
18Note
- ????? getCh ???????? 1 ????????????????????????
- ???????????????????????Hugs ??????????????????????
??????
primitive getCh IO Char
19?? guess ??????????????????? ????????????????????
guess String ? IO () guess word do
putStr "gt " xs ? getLine if xs
word then putStrLn "You got it!"
else do putStrLn (diff word xs)
guess word
20?? diff ??1 ??????????????2 ?????????????????
diff String ? String ? String diff xs ys
if elem x ys then x else '-' x ? xs
?
gt diff "haskell" "pascal" "-as--ll"
21???(9?)
- Haskell ??
- ???? ?????
- ?????? ???????????? IO a ?? a ??????????
- IO Char ????????????
- IO () ???????????????
- ????????
- getChar IO Char 1 ???????????????
- putChar Char -gt IO () 1 ??????????
- return v a -gt IO a ?????????? v ???
- getLine IO String ???????????????
- putStr String -gt IO () ????????? (putStrLn
?????) - ?????????????????
- do x ? p1 p2 y ? pn (??)
22????(9?)
- ??? p.109 ? strlen ?????????????
- ??????????????????????????????
- Hangman.hs ??? Web ??????????http//www.ist.aichi
-pu.ac.jp/lab/yamamoto/program_languages/2011/ - ?????????????????????????????????????????????????
- ????????????????
23??????(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 ??????????????????????
????
24??????(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 ??????
25??????(Windows, ??2)
- ansi-terminal ?????????
- cabal ? Haskell Platform ?????????
- ??????????????
- ?????????????? cabal update
- ansi-terminal ????????? cabal install
ansi-terminal - Windows ? GHCi ????????
- ??????WinGHCi ??????
- calculatorWin.lhs ??????run ??????????????????????
- lifeWin.lhs ?????? life glider ???????????????????
???????
26(No Transcript)
27????
Nim ??????? Haskell ???????????????????
1 2 3 4 5
28- 2 ???????????????? 1 ????????????? 1 ??????????
- ??????????????????
??? ??????????????????????? 5 ???????????????
????????? 5,4,3,2,1 ????