《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还是先看其它一些入门书籍吧,这本书配套着学学还是不错的。

第六章 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是个相当强大的内置函数,它可以在一个列表内的每两个元素之间都插入一个运算符。

如:foldr1 (+) [1..10]就是1+2+3+4+5+6+7+8+9+10,即返回55。

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

2012-11-13 16-28-57

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

用括号把一个运算符包起来,表示是一个函数,如(+)和(.)

如果写成foldr1 . [f, g, h],就会把中间的点解释成一个运算符,从而报错。

所以(+) 2 3也就是指2+3,前面这种表达式在LISP语言中常见。

foldr1 post "haskell"  == 's'

 

2 b

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

函数的中缀表示法:div 5 2 == 5 `div`2。注意那个反向的`符号。

 

3 d

foldr1 f [a, b, c, d]

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

函数f一定有两个参数,两个参数与返回值都是同一种类型,后面学过类型后,f的类型应该是a –> a –> a。以前面的&&为例,a && b等价于(&&) a b,注意&&要用括号引起来。

想看一个函数的类型,可以在ghci(或winghci)中,执行:t 命令,后面跟上函数名称即可,例如 :t foldr1

得到:foldr1 :: (a -> a -> a) -> [a] –> a

 

4 b

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

foldr1 f需要1个参数

 

5 a

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

如第一题讲的,foldr1 (+) [1,2,3,4]中第二个参数是数值的列表

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

 

6 b

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

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

 

7 c

字符串的表示方式与C语言基本上一样,在表示引号时需要用转义符,"is \"hot\" now",有2个空格、2个引号、8个字母,共12个字符

 

 

《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》(二十四)代数类型

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