《Two Dozen Short Lessons in Haskell》(二十四)代数类型

简介:

这是《Two Dozen Short Lessons in Haskell》这本书的最后一章,第23章没有习题。

这一章里介绍了Haskell如果自定义一种类型,并且用一个双人博弈游戏为例子讲解了如何使用这些类型,里面简单介绍了Minimax算法。

至此,这本书全部学完,当然还没用Haskell写过什么大一点的程序,只是掌握了其基本概念。

 

1 A tree, in computer science, is an entity

a with a root and two subtrees

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 roots

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

 

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,不知道为什么不正确?


这是《Two Dozen Short Lessons in Haskell》这本书的最后一章,第23章没有习题。

这一章里介绍了Haskell如果自定义一种类型,并且用一个双人博弈游戏为例子讲解了如何使用这些类型,里面简单介绍了Minimax算法。

至此,这本书全部学完,当然还没用Haskell写过什么大一点的程序,只是掌握了其基本概念。

 

1 A tree, in computer science, is an entity

a with a root and two subtrees

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 roots

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

 

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,不知道为什么不正确?

本文转自申龙斌的程序人生博客园博文,原文链接:http://www.cnblogs.com/speeding/p/3302530.html,如需转载请自行联系原作者

http://www.cnblogs.com/speeding/ 

相关文章
|
存储 程序员 C++
Python中的“Short”类型模拟与理解
Python中的“Short”类型模拟与理解
420 2
|
Java C++
Java - 数据类型 short VS int 转换原理
Java - 数据类型 short VS int 转换原理
462 0
Java - 数据类型 short VS int 转换原理
java中整型数据(byte、short、int、long)溢出的现象及原理
java中整型数据(byte、short、int、long)溢出的现象及原理
|
Linux C语言 存储
printf中的short int, int, long int和long long int
hd: short int d: int ld: long int lld: long long int Linux基本数据类型大小——int,char,long int,long long int 在Linux操作系统下使用GCC进行编程,目前一般处理器为32位字宽,下面是/usr/include/limit.h文件对Linux下数据类型的限制及存储字节大小的说明。
1393 0
|
6月前
|
安全 网络架构
对比外部公网IP与局域网内部IP的差异性
综上所述,外部公网IP地址与局域网内部IP地址在功能、应用范围、安全性与管理方式上存在明显的差异性。公网IP地址为网络设备提供了在整个互联网中可识别的唯一身份,而内网IP仅在私有网络中有效,且安全性相对较高。理解这些差异能有助于更好地配合网络地址的规划、管理与安全策略的设计。
585 8
|
缓存 NoSQL 中间件
应对数据库不断膨胀的数据:缓存和队列中间件
【6月更文挑战第5天】该文探讨了优化数据库使用以提升应用系统性能的策略。文中建议利用Redis缓存和MQ消息队列作为辅助工具,以进一步优化性能和减少资源消耗。
620 2
应对数据库不断膨胀的数据:缓存和队列中间件
|
人工智能 芯片
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
|
Ubuntu Linux
在Linux中,如何查看当前系统的版本信息?
在Linux中,如何查看当前系统的版本信息?

热门文章

最新文章