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

第五章 Function Composition and Currying函数组合与柯里函数

1 Given the following definitions of f and g, the following Haskell command delivers

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

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

HASKELL COMMAND • f(g "A man, a plan, a canal. Panama!")

a syntax error, unexpected parenthesis

b the empty string

c syntax error, type conflict in operands

d xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

 

2 Given the above definitions of f and g, and the following definition of teddy, the following command delivers

HASKELL DEFINITION • teddy = "A man, a plan, a canal. Panama!"

HASKELL COMMAND • (f . g) teddy

a syntax error, unexpected parenthesis

b the empty string

c syntax error, type conflict in operands

d xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

 

3 Given the above definitions of f, g, and teddy, the following Haskell command delivers

HASKELL COMMAND • (f . g) teddy == f(g teddy)

a syntax error, unexpected parenthesis

b the empty string

c True

d xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

 

4 What would be the answer to the preceding question if, in the definitions of f and g, the sense of all the comparisons

had been reversed (not equals instead of equals, less-than instead of greater-than-or-equal, etc.)?

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

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

a syntax error, unexpected parenthesis

b the empty string

c True

d xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

 

5 If equals is a function that requires two arguments, then equals ’x’ is a function that requires

a no arguments

b one argument

c two arguments

d three arguments

 

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

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

 

1 b

函数组合f (g x)不能把括号去掉,f g x会把g当成函数f的参数,但可以写成(f . g) x

g str = [ c | c <- reverse str, c < ’n’ ]把字母n前面的字符都筛选出来,并逆序输出

f str = [ c | c <- str, c == ’x’ ]只输出'x’字符,但很然,前面的小于n的字母里肯定没有x,所以最后的输出结果就是空串[]

 

2 b

(f . g) teddy等同于f ( g teddy),与第一题是一个意思

 

3 c

(f . g) teddy 与 f(g teddy)是等价的,所以(f . g) teddy == f(g teddy)返回True

 

4 c

只要没有语法错,(f . g) teddy 与 f(g teddy)永远相等

 

5 b

书中举的例子非常容易理解,一个函数remove chr str,把字符串str中的某个字符chr去掉,那么remove '$'就是把字符'$'除掉,那么remove '@'就是把字符'@'除掉,remove是二个参数的函数,而remove '$’ 这个函数就只能跟一个参数了。

柯里函数就是从这里来的,有许多与之相关的定理,这个概念需要好好理解一下。在C语言中学的f(a,b)函数是一个整体,但在Haskell中f是函数,f a也是函数,f a b 也是函数。柯里函数简单的理解就是参数不全时的函数。

Haskell语言的名字也来自于这个人Haskell B. Curry。

 

进阶:(.)是一个操作符,源程序是这样的:

(.)    :: (b -> c) -> (a -> b) -> a -> c
(.) f g = \x -> f (g x)

 

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