# 《Two Dozen Short Lessons in Haskell》学习（八）- Function Types, Classes, and Polymorphism

## 第八章 Function Types, Classes, and Polymorphism

1 Polymorphic functions

a change the types of their arguments

b combine data of different types

c can operate on many types of arguments

d gradually change shape as the computation proceeds

2 The function toUpper takes a letter of the alphabet (a value of type Char) and delivers the upper-case version of

the letter. What is the type of toUpper?

a polymorphic

b Char -> Char

c lower -> upper

d cannot be determined from the information given

3 A value of type [a] is

a a sequence with elements of several different types

b a sequence with some of its elements omitted

c a sequence whose elements are also sequences

d a sequence whose elements are all of type a

4 A function of type [a] -> [[a]] could

a transform a character into a string

b deliver a substring of a given string

c deliver a string like its argument, but with the characters in a different order

d transform a string into a sequence of substrings

5 Suppose that for any type a in the class Ord, pairs of values of type a can be compared using the operator <.

A function of type Ord a => [a] -> [a] could

a rearrange the elements of a sequence into increasing order

b deliver a subsequence of a given sequence

c both of the above

d none of the above

6 Suppose Ord is the class described in the preceding question. What is the type of the operator <.

a Ord a => a -> a -> Bool

b Ord a => a -> Bool

c Ord a => a -> Char

d Ord a => a -> [Char]

7 The equality class

b is what makes functions possible

c is what makes comparison possible

d excludes function types

8 A function with the type Eq a => a -> Bool

a requires an argument with the name a

b delivers True on arguments of type a

c is polymorphic

d must be equal to a

9 If the type of f has three arrows in it, then the type of f x has

a one arrow in it

b two arrows in it

c three arrows in it

d four arrows in it

10 A polymorphic function

a has more than one argument

b has only one argument

c may deliver values of different types in different formulas

d can morph(改变) many things at once

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

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

1 c

combline data of different types，多态函数并不负责将多种类型的数据组合在一起，后面讲的tuple元组能够完成这个任务。

2 b

toUpper只能接受Char类型的数值，并且得到Char类型（大写字母）的数值，所以类型就是Char –> Char

3 d

4 d

5 c

f :: Ord a => [a] -> [a]

[a] -> [a]表示接受一个参数，这个参数是一个列表，列表中的元素可以排序。返回值也是一个列表，列表中的元素当然也支持排序。

6 a

Ord中的类型可以比较大小，函数(<)接收2个输入参数，返回布尔值。

(<) :: Ord a => a -> a -> Bool

7 d？

8 c

Eq a => a –> Bool 这里面有a这样的符号，表示是一种多态函数，a是一种Eq中的一种类型。这个函数接收一个参数，这个参数可以判断是否与其它实例相等（即可以进行'=='这种运算符操作），返回值是一个布尔值。

isPalindrome :: Eq a => [a] –> Bool

isPalindrome phrase = (phrase == reverse phrase)

9 b

f有三个箭头，则f x的类型就有两个箭头。

f :: Char –> Char –> Char –> [Char]

f x y z = [ x, y, z]

f x :: Char –> Char –> [Char]

10 c

《Two Dozen Short Lessons in Haskell》学习（一）Hello World

《Two Dozen Short Lessons in Haskell》学习（二）Definitions

《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》学习（二十二）- 递归

《Two Dozen Short Lessons in Haskell》（二十四）代数类型

posted @ 2012-11-21 15:59  申龙斌的程序人生  阅读(1171)  评论(0编辑  收藏  举报