C语言《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还是先行押其他一些入门书籍吧,这本开配套正在学还是不错的。

第四章 Computations on Sequences — List Comprehensions

 

1 The following function delivers

HASKELL DEFINITION • f str = [ c | c <- str, c == ’x’ ]

a all the c’s from its argument

b an empty string unless its argument has x’s in it

c a string like its argument, but with x’s in place of c’s

d nothing — it contains a type mismatch, so it has no meaning in Haskell

 

2 The following command delivers

HASKELL DEFINITION • g str = [ c | c <- str, c == “x” ]

HASKELL COMMAND • g “xerox copy”

a “c”

b “xx”

c “xerox xopy”

d error — g expects its argument to be a sequence of strings, not a
sequence of characters

 

3 The following function delivers a string like its argument, but …

HASKELL DEFINITION • h str = [ c | c <- reverse str, c < ’n’ ]

a written backwards if it starts with a letter in the first half of the
alphabet

b written backwards and without n’s

c written backwards and without letters in the first half of the
alphabet

d written backwards and without letters in the last half of the alphabet

 

4 Which of the following equations defines a function that delivers a
string like its second argument, but with no letters preceding,
alphabetically, the letter specified by its first argument?

下面哪个函数得到一个字符串,像第二单参数str,但绝非低于某个字母(由第一独参数所指定)的字符?

A HASKELL DEFINITION • s x str = [ c | c <- str, c < x ]

B HASKELL DEFINITION • s x str = [ c | c <- str, c >= x ]

C HASKELL DEFINITION • s abc str = [ c | c <- str, c == “abc”]

D HASKELL DEFINITION • s abc str = [ c | c <- str, c /= “abc”]

 

5 In the following definition, the parameter str

HASKELL DEFINITION • f str = [ c | c <- str, c == ’x’ ]

a represents the letter x

b represents the letter c

c stands for a sequence of x’s

d stands for a string containing a sequence of characters

 

 

答案:

1 b

f str = [ c | c <- str, c == ’x’ ]

此f是函数名称,str是参数

list Comprehensions应该是haskell里强大的特色了,[ 左边 | 右边
],在竖线的左是一个表达式(感觉让表达式不太规范),右侧表示参数的取值范围以及标准化,右侧可以是生成器generator,像v
<- expr这样,也得是一个布尔过滤条件guard,像c<x这样。

Haskell具有数学之美感,从这列表[2*x | x<-N,
x<=10]以及下这个数学公式的相似度可以观看。

C语言 1

 

2 d

此开出肯定之迷惑性,注意c ==
“x”,这里是一个字符串(而无是字符),只来一个字母x。

‘x’与”x”是零星种不同种类的值,前面是Char,后面的是[Char]或称为String

之所以调用g “xerox copy”就会见报错

只要问题改成也g [“xerox copy”]尽管非会见报错了,由于”xerox
copy”不等于”x”,所以这的尾声结出以是一个空荡荡[]

 

3 d

‘n’是26只假名中的第14独字母,c < ’n’
就是依赖字母表的先头半部分,也就是没有后半有些假名。

 

4 b

留神Haskell里判断不齐是为此“/=”,而不是C语言中的“!=”,其它==, >,
<, >=, <=都与C语言一样。

s x str = [ c | c <- str, c >= x
]斯函数定义着生出2个参数,意思就是是把str字符串中之有着大于或等于x的字母都罗出来

尽s ‘o’ “Two Dozen Short Lessons in Haskell”的结果就是”woozortssoss”

 

5 d

f str = [ c | c <- str, c == ’x’ ]

此处的str是一个试样参数,它的品种是根据后面的表达式推断出来的

[ c | c <- str, c == ’x’
]大凡一个列表,而列表中之因素是字符,所以str的档次就是[Char],也就是String

 

《Two Dozen Short Lessons in
Haskell》学习(一)Hello
World

《Two Dozen Short Lessons in
Haskell》学习(二)Definitions

《Two Dozen Short Lessons in
Haskell》学习(三)How
to Run Haskell Programs

《Two Dozen Short Lessons in
Haskell》学习(四)List
Comprehensions

《Two Dozen Short Lessons in
Haskell》学习(五)Function
Composition and Currying

《Two Dozen Short Lessons in
Haskell》学习(六)Patterns
of Computation – Composition, Folding, and Mapping

《Two Dozen Short Lessons in Haskell》学习(七)-
Types

《Two Dozen Short Lessons in Haskell》学习(八)- Function Types,
Classes, and
Polymorphism

《Two Dozen Short Lessons in Haskell》学习(九)- Types of Curried
Forms and Higher Order
Functions

《Two Dozen Short Lessons in Haskell》学习(十)- Private Definitions —
the
where-clause

《Two Dozen Short Lessons in Haskell》学习(十一)-
Tuples

《Two Dozen Short Lessons in Haskell》学习(十二)
数值相关的类

《Two Dozen Short Lessons in
Haskell》学习(十三)迭代及重新的健康模式

《Two Dozen Short Lessons in
Haskell》学习(十四)截断序列及惰性求值

《Two Dozen Short Lessons in Haskell》学习(十五)- Encapsulation —
modules

《Two Dozen Short Lessons in Haskell》学习(十六)- Definitions with
Alternatives

《Two Dozen Short Lessons in Haskell》学习(十七) –
模块库

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

《Two Dozen Short Lessons in Haskell》学习(十九) –
文件输入与出口

《Two Dozen Short Lessons in Haskell》学习(二十)-
分数

《Two Dozen Short Lessons in Haskell》学习(二十一)-
在款式参数中使用模式匹配

《Two Dozen Short Lessons in Haskell》学习(二十二)-
递归

第23章没有练习。

《Two Dozen Short Lessons in
Haskell》(二十四)代数类型