《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 The Haskell class Fractional includes
a? integral, real, and complex numbers
b? numbers between zero and one, but not numbers bigger than one
c? both floating point and rational numbers
d? the Mandelbrot set 指的分形圖形
?
2 The mantissa of a floating point number determines?
a? where the decimal point goes
b? the range of the number and its sign
c? the magnitude and precision of the number
d? the sign of the number and the digits in its decimal numeral
?
3? The exponent of a floating point number determines
a where the decimal point goes
b the range of the number and its sign
c the magnitude and precision of the number
d the sign of the number and the digits in its decimal numeral
?
4? The following denote floating point numbers as the should appear in a Haskell script
a 1.89533e+25, 18.01528974, 1.05522e-24, +27.0
b 1.89533 × 1025, 18.01528974, 1.05522 × 10-24, -27.0
c 1.89533e+25, 18.01528974, 1.05522e-24, -27.0
d all of the above
?
5? Analog to digital conversion converts a number
a? from a set containing a great many numbers to a number from a much smaller set
b? to zero or one
c? to a pattern of zeros and ones
d? by a digital analogy process
?
6? Which of the following formulas would useful for analog to digital conversion?
a? floor((x - a)/dx)
b? floor(n?(x - a)/(b - a))
c? floor . (/ dx) . (+(- a))
d? all of the above
?
7? Numbers of type Rational in Haskell scripts are
a? compatible with floating point numbers in arithmetic operations
b? constructed from two integers by putting a percent-sign between them
c? especially useful when precision is not the most important factor
d? all of the above
=============================
下
面
是
答
案
=============================
1 c
書中答案是a,不知道是怎么回事?
?
2 d
類型Float和Double的數都包括2個部分:mantissa小數部分,exponent指數部分。
小數部分決定了該數的正負號和有效數字個數。
關于詳情可以看看IEEE754浮點數表示。
?
3 a
指數部分決定了小數點的位置。
?
4 c
+27.0是錯誤的表示法,不能把加號放在前面。另外1. 和 .1 也都不是正確的表示法。
?
5 a
模數轉換就是把范圍很大的數轉換到一定范圍內的數。
?
6 d
有點迷惑性,書中的公式對應著答案a。
答案c是另一種表示法。
答案b是把dx=(b-a)/n代入a中。
?
7 b
Haskell的分數可以用兩個整數,在中間放一個百分號來表示。例如:3%5,5%3,-232%365
直接在ghci中輸入上面的表達式會出現Not in scope: `%'錯誤,需要加載Data.Ratio模塊。
:m Data.Ratio