《Two Dozen Short Lessons in Haskell》学习(十六)- Definitions with Alternatives
?《Two Dozen Short Lessons in Haskell》(Copyright ? 1995, 1996, 1997 by Rex Page,有人翻譯為Haskell二十四學(xué)時(shí)教程,該書如果不用于贏利,可以任意發(fā)布,但需要保留他們的copyright)這本書是學(xué)習(xí) Haskell的一套練習(xí)冊,共有2本,一本是問題,一本是答案,分為24個章節(jié)。在這個站點(diǎn)有PDF文件。幾年前剛開始學(xué)習(xí)Haskell的時(shí)候,感覺前幾章還可以看下去,后面的內(nèi)容越來越難以理解。現(xiàn)在對函數(shù)式編程有了一些了解后,再來看這些題,許多內(nèi)容變得簡單起來了。
初學(xué)Haskell之前一定要記住:
把你以前學(xué)習(xí)面向過程的常規(guī)的編程語言,如Pascal、C、Fortran等等統(tǒng)統(tǒng)忘在腦后,函數(shù)式編程完全是不一樣的編程模型,用以前的術(shù)語和思維來理解函數(shù)式編程里的概念,只會讓你困惑和迷茫,會嚴(yán)重地影響你的學(xué)習(xí)進(jìn)度。
這個學(xué)習(xí)材料內(nèi)容太多,想把整書全面翻譯下來非常困難,只有通過練習(xí)題將一些知識點(diǎn)串起來,詳細(xì)學(xué)習(xí)Haskell還是先看其它一些入門書籍吧,這本書配套著學(xué)學(xué)還是不錯的。
第16章 帶條件分支的函數(shù)定義
1 Guards in function definitions
a hide the internal details of the function from other software components
b remove some of the elements from the sequence
c select the formula that delivers the value of the function
d protect the function from damage by cosmic rays
?
2 The formula map reverse ["able", "was", "I"] delivers the value
a ["I", "saw", "elba"]
b ["elba", "saw", "I"]
c ["I", "was", "able"]
d ["able", "was", "I", "not"]
?
3 The formula map f xs delivers the value
a f x
b [f x | x <- xs]
c f xs
d [f xs]
?
4 Which of the following formulas is equivalent to the formula [g x y | y <- ys] ?
a (map . g x) ys
b (map g x) ys
c map (g x y) ys
d map (g x) ys
?
5 The following function delivers
HASKELL DEFINITION ? h xs
HASKELL DEFINITION ? ? ? | xs == reverse xs = "yes"
HASKELL DEFINITION ? ? ? | otherwise = "no"
a "yes", unless xs is reversed
b "yes" if its argument is a palindrome, "no" if it’s not
c "no" if xs is not reversed
d "yes" if its argument is written backwards, "no" if it’s not
?
6 The following function
HASKELL DEFINITION ? s x
HASKELL DEFINITION ? ? ? | x < 0 ? ?= -1
HASKELL DEFINITION ? ? ? | x == 0 = 0
HASKELL DEFINITION ? ? ? | x > 0 ? ?= 1
a the value of its argument
b the negative of its argument
c a code indicating whether its argument is a number or not
d a code indicating whether its argument is positive, negative, or zero
?
7 Assuming the following definitions, which of the following functions puts in?sequence of x’s in place of all occurrences of a given word in a given?sequence of words?
HASKELL DEFINITION ? rep n x = [ x | k <- [1 . . n]]
HASKELL DEFINITION ? replaceWord badWord word
HASKELL DEFINITION ? ? ? | badWord == word = rep (length badWord) ’x’
HASKELL DEFINITION ? ? ? | otherwise = word
a censor badWord = map (replaceWord badWord)
b censor badWord = map . replaceWord badWord
c censor badWord = replaceWord badWord . map
d censor badWord = map badWord . replaceWord
?
=========================================================
答
案
在
下
面
=========================================================
1 c
在函數(shù)定義里還可以有分支,當(dāng)滿足某種條件時(shí),用一種定義,當(dāng)滿足另外的條件時(shí),用另外一套定義。有點(diǎn)像C語言中的switch語句。
f x| x > 0 = 1| otherwise = -1?
2 b
這道題容易做錯。
先想想reverse ["able", "was", "I"]的結(jié)果是什么就不容易做錯了。
以前提到過reverse函數(shù)的意思是把一個列表中的元素倒序排列,?["able", "was", "I"]這個里面有三個元素,倒過來就是[“I”, "was", "able"]。
那么map reverse函數(shù)的功能就可以這樣描述了:把一個列表xsFather中的每個元素xsChild(本身還是一個列表)里的內(nèi)容倒序。上例中xsFather就是["able", "was", "I"],第一個元素是xsChild就是“able”,倒過來就是"elba",這樣map函數(shù)的意思也就清楚了,就是把reverse函數(shù)都作用于每個元素。
?
3 b
這里就是map函數(shù)的定義:
map f xs = [f x | x <- xs]?
每一個參數(shù)f本身也是個函數(shù),它要作用于后面xs里的每個元素x上,即f x。
第二個參數(shù)xs是一個列表,實(shí)際上就是把函數(shù)f遍歷作用于xs中的元素上。
?
4 d
根據(jù)柯里函數(shù)的定義,g x也是一個函數(shù),不妨記為h,那么
map (g x) ys = map h ys = [ h y | y <- ys]
這樣應(yīng)該好理解一些了。
對于柯里函數(shù)搞不懂的,需要認(rèn)真學(xué)習(xí)高階函數(shù)和不全函數(shù)等概念,一開始挺難理解的:-(?
參見:《Two Dozen Short Lessons in Haskell》學(xué)習(xí)(九)- Types of Curried Forms and Higher Order Functions
?
5 b
就是一個回文序列的判斷函數(shù),如果是回文,就返回"yes",否則返回"no"
h xs| (xs == reverse xs) = "yes" -- 當(dāng)一個序列如果等于它的反序時(shí)| otherwise = "no"加上括號就好理解一些了。
在haskell中這個豎線就叫g(shù)uard,注意縮進(jìn)。每一條豎線后面跟著一個布爾表達(dá)式,當(dāng)它滿足時(shí)就用=后面的定義,不滿足時(shí),繼續(xù)判斷下一個guard條件。?
?
6 d
這個很簡單,就是通常的求一個數(shù)的正負(fù)符號的函數(shù)。
?
7 a
rep n x = [ x | k <- [1 . . n]] 這個函數(shù)可以把x重復(fù)n次,這里的x并沒有說是什么類型。
例如:rep 3 2 就等于[2,2,2]
rep 3 '2'就是['2', '2', '2'],即"222"
guard條件是: ? ? | badWord == word
緊接的定義是: ?= rep (length badWord) ’x’
就是把badWord全換為字符'x'
replaceWord "fuucck" "fuucck"就等于"xxxxxx"
這個函數(shù)可以用于去掉文章中不文雅的詞。?
轉(zhuǎn)載于:https://www.cnblogs.com/speeding/archive/2013/03/05/2934518.html
總結(jié)
以上是生活随笔為你收集整理的《Two Dozen Short Lessons in Haskell》学习(十六)- Definitions with Alternatives的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [C.Sharp] 接口函数还可以声明为
- 下一篇: Entity Framework 6 A