《Two Dozen Short Lessons in Haskell》(二十四)代數類型

這是《Two Dozen Short Lessons in Haskell》這本書的最後一章,第23章沒有習題。git

這一章裏介紹了Haskell若是自定義一種類型,而且用一個雙人博弈遊戲爲例子講解了如何使用這些類型,裏面簡單介紹了Minimax算法。算法

至此,這本書所有學完,固然還沒用Haskell寫過什麼大一點的程序,只是掌握了其基本概念。api

 

1 A tree, in computer science, is an entity數據結構

a with a root and two subtreesdom

b with a root and a collection of subtrees, each of which is also a tree函數

c with a collection of subtrees, each of which has one or more rootsspa

d described in a diagram with circles, lines, and random connectionsorm

 

2 A sequence, in Haskell, is an entity對象

a with one or more elements遊戲

b that is empty or has a first element followed by a sequence of elements

c whose elements are also sequences

d with a head and one or more tails

 

3 The following definition specifies

HASKELL DEFINITION • data WeekDay =

HASKELL DEFINITION • Monday | Tuesday | Wednesday | Thursday | Friday

a a type with five constructors

b a type with five explicit constructors and two implicit ones

c a tree with five roots

d a sequence with five elements

 

4 Given the definition in the preceding question, what is the type of the following function f?

HASKELL DEFINITION • f Tuesday = "Belgium"

a f :: WeekDay –> String

b f :: Tuesday -> "Belgium"

c f :: Day –> Country

d type of f cannot be determined

 

5 Types defined in Haskell scripts with the data keyword

a must begin with a capital letter

b may be imported from modules

c must be used consistently in formulas, just like intrinsic types

d all of the above

 

6 What kind of structure does the following type represent?

HASKELL DEFINITION • data BinaryTree = Branch BinaryTree BinaryTree | Leaf String

a a type with four constructors

b a digital structure

c a tree made up of ones and zeros

d a tree in which each root has either two subtrees or none

 

7 Given the preceding definition of the type BinaryTree, which of the following defines a function that computes

the total number of Branch constructors in an entity of type BinaryTree?

a branches binaryTree = 2

b branches (Branch left right) = 2

branches (Leaf x) = 0

c branches (Branch left right) = 1 + branches left + branches right

branches (Leaf x) = 0

d branches (Branch left right) = 2*branches left + 2*branches right

branches (Leaf x) = 1

 

8 The formula xs!!(length xs - 1)

a is recursive

b has the same type as xs

c delivers the same result as last xs

d none of the above

 

9 Given the definition of the function pam in the module SequenceUtilities, the formula

pam (map (+) [1 . . 5]) 10

a delivers the same result as map (1+) [1 . . 5]

b delivers the same result as pam [1 .. 5] (map (1+))

c delivers the result [11, 12, 13, 14, 15]

d all of the above

 

10 Given the Grid [1,3,0, 0,0,0, 0,0,2] (as in the tic-tac-toe script), what is the status of the game?

a game over, X wins

b game over, O wins

c O’s turn to play

d X’s turn to play

 

11 Which of the following formulas extracts the diagonal of a grid (as in the tic-tac-toe program)?

a (take 3 . map head . iterate(drop 4)) grid

b [head grid, head(drop 4 grid), head(drop 8 grid)]

c [head grid, grid!!4, last(grid)]

d all of the above

 

======

答案

======

 

 

1 b

數據結構中「樹」的定義,一個根及多個子樹

 

2 b

列表有多是空的

 

3 a

data能夠來自定義一種類型(也就是面嚮對象語言中的類),這裏有五個構造器constructor。只是這裏的構造器都沒有帶參數。

 

4 a

這個簡單,定義好的類型能夠像內置的Haskell類型同樣來用。

 

5 d

類型名稱必須是大寫字母開頭的;能夠導入模塊中;能夠像內置的類型同樣用。

 

6 d

就是二叉樹的定義

 

7 c

求二叉樹的分支總數。葉子節點分支爲0,不然爲左子樹的分支數+右子樹的分支數。

 

8 c

!!用來取出一個列表中指定位置上的元素。這裏就是取最後一個元素,與last函數同樣。

 

9 c?

還沒搞明白pam的意思。

 

10 c

先走畫X,後走的畫O。該O方走棋。

130   XX.

000   …

002   ..O

 

11 b?

答案是b,我認爲是d。感受這些表示方法都正確,答案a中的函數寫得至關牛X,不知道爲何不正確?

相關文章
相關標籤/搜索