《Two Dozen Short Lessons in Haskell》学习(七)- Types

 

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

第7章 Types

 

1 The type of [[True, False], [True, True, True], [False]] is

a mostly True

b ambiguous

c [Bool]

d [[Bool]]

 

2 The type of ["alpha", "beta", "gamma"] is

a [[Char]]

b [String]

c both of the above

d none of the above

 

3 The type of [["alpha", "beta", "gamma"], ["psi", "omega"]] is

a [[String]]

b [[Char]]

c [String]

d [String, String]

 

4 The formula foldr1 (.) [f, g, h, k] delivers

a a string

b a function

c a sequence

d nothing — it’s an error because k can’t be a function

 

5 Which of the following is a sequence whose elements are sequences

a [ "from", "the", "right" ]

b [ [ "Beyond", "Rangoon" ], [ "Belle", "du", "Jour" ] ]

c [ [ 'a' ], [ 'b' ], [ 'c' ] ]

d all of the above

 

6 The type of the formula foldr1 (.) [f, g, h, k] is

a String -> String

b the same as the type of f

c the same as the type of the composition operator

d [a] -> [b]

 

7 If the type of f is String -> String, then the type of [f x | x <- xs] is

a String -> String

b String

c [String]

d none of the above

 

8 The Haskell entity [ [ "Beyond", "Rangoon" ], [ "Belle", "du", "Jour" ] ] has type

a [ [ String ] ]

b [ [ [ Char ] ] ]

c both of the above

d none of the above

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

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

1 d

Haskell是一种类型要求非常严格的语言!也就是说类型不匹配,将无法通过编译器或解释器。

[[True, False], [True, True, True], [False]] 从最外一层看,是一个List,所以类型应该是[?],中间是什么类型,需要看每个逗号之间是什么数据。

[True, False]本身也是一个List,类型是[Bool],同理后面几个也是[Bool]类型

所以整个列表的类型就是[[Bool]]

 

2 c

由于String与[Char]是等价的,所以答案是c

 

3 a

[[[Char]]]也正确

 

4 b

foldr1 (.) [f, g, h, k]把一些函数连接在一起,还是一个函数

 

5 d

"from","the"以及"right"都是Char的列表,这里经常把List称为Sequence

 

6 b

这道题有点难

把操作符放在括号里面就变成了一个函数,(.)函数是一个连接2个函数的函数,是有点难以理解。

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

上面就是(.)的类型,需要仔细地理解一番。

由于[f, g, h, k]是一个列表,所以f,g,h,k一定具有相同的类型,类型就应该是a->a->a

把以foldr1 (.) [f, g, h, k]的类型就与f,g,h,k这些函数的类型是一样的

 

7 c

f类型是String –> String,那么f x就是String类型,[f x | x <- xs]就是[String]

 

8 c

"Beyond"是String类型

[ "Beyond", "Rangoon" ]是[String]类型

所以[ [ "Beyond", "Rangoon" ], [ "Belle", "du", "Jour" ] ]就是[[String]]类型,也就是[[[Char]]]

 

《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-19 20:05  申龙斌的程序人生  阅读(1227)  评论(1编辑  收藏  举报