《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还是先看其它一些入门书籍吧,这本书配套着学学还是不错的。
1 Software libraries
a contain functions encapsulated in modules
b provide a way to package reusable software
c both of the above
d none of the above
2 A module that supplies reusable software should
a export all of the functions it defines
b import all of the functions it defines
c export reusable functions, but prevent outside access to functions of limited use
d import reusable functions, but avoid exporting them
3 The formula concat ["The", "Gold", "Bug"] delivers
a "The Gold Bug"
b ["The", "Gold", "Bug"]
c "TheGoldBug"
d [["The], ["Gold"], ["Bug"]]
4 Encryption is a good example to study in a computer science course because
a it is an important use of computers
b it involves the concept of representing information in different ways
c both of the above
d well … really … it’s a pretty dumb thing to study
5 The DES cipher is a block cipher. A block cipher is
a a substitution cipher on a large alphabet
b a rotation cipher with scrambled internal cycles
c less secure than a substitution cipher
d more secure than a substitution cipher
6 Professor Dijkstra thinks that in the software development profession
a mathematical ability is the only really important asset that programmers need
b the ability to express oneself in a natural language is a great asset to programmers
c mathematical ability doesn’t have much influence on a programmer’s effectiveness
d it’s a waste of time to prove, mathematically, the correctness of program components
=========================================================
答
案
在
下
面
=========================================================
1 c
Haskell已经构建了一个庞大的函数库,可以import这些模块来达到重用之目的。
2 c
一个模块为了重用,可以让其它模块使用它的函数,但一些内部实现细节不让访问。
以前说过,下面这个模块定义:
module ModuleName (function1, function2)
括号内的函数才对外公开。
3 c
concat函数可以把一个列表中的元素连接在一起,定义如下:
concat :: [[a]] -> [a]
concat [[1,2,3], [4,5], [6,7,8,9]] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
concat的实际内部定义是这样的:
concat = foldr (++) [ ]
就是这个意思:[[1,2,3], [4,5], [6,7,8,9]] = [1,2,3] ++ [4,5] ++ [6,7,8,9] ++ [] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
4 c
学习计算机的人们总要接触一些加密方面的知识。
5 a
有关DES加密方面的资料,可以参考百度百科。
6 b
书中关于Dijkstra的原话是:
Besides a mathematical inclination, an exceptionally good mastery of one's native tongue is the most vital asset of a competent programmer.
除了数学爱好,对于一个有能力的程序员来说,出色地掌握自己的母语是最宝贵的财富。