《Two Dozen Short Lessons in Haskell》学习(十八) - 交互式键盘输入和屏幕输出

《Two Dozen Short Lessons in Haskell》(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学时教程,该书如果不用于赢利,可以任意发布,但需要保留他们的copyright)这本书是学习 Haskell的一套练习册,共有2本,一本是问题,一本是答案,分为24个章节。在这个站点有PDF文件。几年前刚开始学习Haskell的时候,感觉前几章还可以看下去,后面的内容越来越难以理解。现在对函数式编程有了一些了解后,再来看这些题,许多内容变得简单起来了。

初学Haskell之前一定要记住:

把你以前学习面向过程的常规的编程语言,如Pascal、C、Fortran等等统统忘在脑后,函数式编程完全是不一样的编程模型,用以前的术语和思维来理解函数式编程里的概念,只会让你困惑和迷茫,会严重地影响你的学习进度。

这个学习材料内容太多,想把整书全面翻译下来非常困难,只有通过练习题将一些知识点串起来,详细学习Haskell还是先看其它一些入门书籍吧,这本书配套着学学还是不错的。

第18章 Interactive Keyboard Input and Screen Output

IO类型在初学Haskell的时候是一个很难理解的概念,平常的编程语言中已经习惯了输入、输出语句,但在函数式编程中一切皆函数,一个确定的函数会得到确定的计算结果,而与操作系统交互时函数式编程就不太方便了,这时Haskell引出了一个IO类型。

一个重要的do表达式:

这一章还介绍了一个字符串连接的函数,可以把字符串末尾加上换行符,再连接起来

unlines :: [String] -> String

unlines = concat . map (++ "\n")

例如:

unlines ["line1", "line2", "line3"] = "line1\nline2\nline3\n"

 

1 Values of IO type

a are in the equality class Eq

b specify requests for operating system services

c represent tuples in a unique way

d describe Jovian satellites 

 

2 Which of the following intrinsic functions in Haskell causes output to appear on the screen?

a concat :: [[any]] -> [any]

b putStr :: String -> IO ()

c printString :: Message –> Screen

d getLine :: IO String 

 

3 What will be the effect of the command main, given the following script?

HASKELL DEFINITION •main =

HASKELL DEFINITION •   do putStr "Good "

HASKELL DEFINITION •        putStr "Vibrations\n"

HASKELL DEFINITION •        putStr " by the Beach Boys\n"

a one line displayed on screen

b two lines displayed on screen

c three lines displayed on screen

d audio effects through the speaker 

 

4 What will be the effect of the command main, given the following script?

HASKELL DEFINITION • main =

HASKELL DEFINITION •   do putStr "Please enter your first and last name (e.g., John Doe): "

HASKELL DEFINITION •        firstLast <- getLine

HASKELL DEFINITION •        putStr (reverse firstLast)

a display of name entered, but with the last name first

b display of last name only, first name ignored

c display of last name only, spelled backwards

d display of name spelled backwards (书中在这里有印刷错误) 

 

5 How should the last input/output directive in the preceding question be changed to display the first name only?

什么时候只输出名字?

a putStr(take 1 firstLast)

b putStr(drop 1 firstLast)

c putStr(takeWhile (/= ’ ’) firstLast)

d putStr(dropWhile (/= ’ ’) firstLast) 

 

=========================================================

=========================================================

1 b 

 

2 b

putStr函数返回的类型是IO (),表示要与操作系统有交互动作,这个()表示不返回任何数据。

 

3 b

结果应该是:

Good Vibrations

 by the Beach Boys

 

4 d

如果输入是John Doe

则屏幕输出:eoD nhoJ

 

5 c

如果输入是John Doe

选项a:take 1 firstLast,会取出第一个字符,“J" 

选项b:drop 1 firstLast,会除掉第一个字符, "ohn Doe"

选项c:takeWhile (/=' ') firstLast,会得到空格前的字符串,"John",是正确答案。

选项d:dropWhile (/=' ') firstLast,会除掉第一个空格前的所有字符," Doe",注意Doe前面还有一个空格 

posted @ 2013-03-23 09:29  申龙斌的程序人生  阅读(889)  评论(0编辑  收藏  举报