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

## 第九章 柯里函数和高阶函数

max 5 6这个函数可以理解为(max 5) 6这样的函数，如果定义一个函数g = max 5，那么g 6与max 5 6就是等价的了，经过这么一番折腾，可以看出每个函数实质上都可以看作为一个参数的函数。max 5 6看上去是2个参数，但实际上可以转化为g 6这样一个参数的函数，而函数g是max 5这样一个函数，也只有一个参数。

1 Suppose functions f and g have types Char -> String and String -> [String], respectively.

Then their composition g . f has type

a Char -> String

b Char -> String -> [String]

c Char -> [String]

d [[String]]

2 Suppose the type of a function f is f :: String -> String -> Bool. Then the type of f "x" is

a Bool

b String

c String -> Bool

d Nothing — f "x" is not a proper formula

3 Suppose the type of a function f is f :: Char -> String -> [String] -> [[String]].

Then f ’x’ and f ’x’ "y" have, respectively, types

a [String] -> [[String]] and [[String]]

b Char -> String -> [String] and Char -> String

c String -> [String] -> [[String]] and [String] -> [[String]]

d Nothing — f ’x’ is not a proper formula

4 Because the arrow notation is right associative, the type a -> b -> c has the same meaning as

a (a -> b) -> c

b a -> (b -> c)

c (a -> b -> c)

d a -> b (-> c)

5 The composition operator has type (.) :: (a -> b) -> (c -> a) -> (c -> b). Another way to express this type is

a (.) :: a -> b -> (c -> a) -> (c -> b)

b (.) :: (a -> b) -> (c -> a) -> c -> b

c (.) :: a -> b -> c -> a -> (c -> b)

d (.) :: a -> b -> c -> a -> c –> b

6 The composition operator has type (.) :: (a -> b) -> (c -> a) -> (c -> b). Another way to express this type is

a (.) :: (b -> c) -> (a -> b) -> (a -> c)

b (.) :: (a -> c) -> (b -> a) -> (b -> c)

c (.) :: (c -> a) -> (b -> c) -> (b -> a)

d all of the above

7 A function whose type is (a -> b) -> c must be

a lower order

b middle order

c higher order

d impossible to define

8 If a function f has type f :: a -> a, then the formulas f ’x’ and f True have, respectively, types

a Char and Bool

b [Char] and [Bool]

c Char -> Char and Bool -> Bool

d cannot be determined from given information

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

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

1 c

(g . f) x = g (f x)

f x把Char变为String，g (f x)再把String变为[String]，那么(g . f)的类型就是Char –> [String]

2 c

3 c

4 b

5 b

(.)函数相当神奇，一开始理解还真需要下点功夫。

对应于f       对应于g

6 d

7 c

8 a

《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-27 20:35  申龙斌的程序人生  阅读(1372)  评论(0编辑  收藏  举报