# 《Two Dozen Short Lessons in Haskell》学习（六）

## 第六章 Patterns of Computation — Composition, Folding, and Mapping

1 Suppose that post is a function that, given two letters, chooses the one that follows the other in the alphabet

(post ’x’ ’y’ is ’y’; post ’u’ ’p’ is ’u’). Then the formula foldr1 post string delivers

a the letter from string that comes earliest in the alphabet

b the letter from string that comes latest in the alphabet

c the first letter from string

d the last letter from string

2 Suppose that && is an operator that, given two Boolean values, delivers True if both are True and False otherwise.

Then the formula foldr1 (&&) [False, True, False, True, True] delivers the value

a True

b False

c Maybe

d Nothing — the formula doesn’t make sense

3 In the formula foldr1 f [a, b, c, d]

a a, b, c, and d must have the same type

b f must deliver a value of the same type as its arguments

c f must be a function that requires two arguments

d all of the above

4 If f is a function that requires two arguments, then foldr1 f is a function that requires

a no arguments

b one argument

c two arguments

d three arguments

5 The second argument of foldr1 must be

a a sequence

b a function

c a sequence of functions

d a function of sequences

6 If next is a function that, given a letter, delivers the next letter of the alphabet, then the mapping process in the

formula [next c | c <- "hal"] delivers the string

a "lah"

b "ibm"

c "alm"

d "lha"

7 The string "is \"hot\" now"

a has four quotation marks in it

b has exactly two spaces and two back-slashes

c has 12 characters, including exactly two spaces

d has 14 characters, including exactly two spaces

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

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

1 b

foldr1是个相当强大的内置函数，它可以在一个列表内的每两个元素之间都插入一个运算符。

fold后面的字母r表示这个运算符是向右连接的（如何解释？）。

Haskell的强大在于这个列表不仅仅指数值，还可以把函数连接起来，如foldr1 (.) [f, g, h]就是(f . g . h)，这里的f,g,h都是函数

2 b

foldr1 (&&) [False, True, False, True, True]就是False && True && False && True &&  True，就是“与”运算，返回False

3 d

foldr1 f [a, b, c, d]

[a, b, c, d]列表必须要保证里面的元素都是同一种类型，[1, “a”]会报错

4 b

foldr1有几个参数与f是无关的，foldr1总是需要2个参数，第一参数是一个函数（没错，函数也可以做为参数），第二个参数是一个列表

foldr1 f需要1个参数

5 a

foldr1的第二个参数是一个列表，这个列表可以是数值，也可以是函数

foldr1 (.) [f, g, h]中第二个参数是函数的列表，f, g, h都是函数

6 b

next得到一个字母在字母表中的下一个字母

[next c | c <- "hal"]对于"hal"中的每一个字符c，都调用 next c 函数，得到"ibm"

7 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-15 07:06  申龙斌的程序人生  阅读(521)  评论(0编辑  收藏  举报