1. 程式人生 > >《Two Dozen Short Lessons in Haskell》(二十四)代數型別

《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代數型別

這是《Two Dozen Short Lessons in Haskell》這本書的最後一章,第23章沒有習題。 這一章裡介紹了Haskell如果自定義一種型別,並且用一個雙人博弈遊戲為例子講解了如何使用這些型別,裡面簡單介紹了Minimax演算法。 至此,這本書全部學完,當然還沒用Haskell寫過什麼

Two Dozen Short Lessons in Haskell》所有習題的索引

《Two Dozen Short Lessons in Haskell》(Copyright © 1995, 1996, 1997 by Rex Page,有人翻譯為Haskell二十四學時教程,該書如果不用於贏利,可以任意釋出,但需要保留他們的copyright)這本書是學習 Haskell的一套練習冊,共

Android開發系列:Notification的功能與使用方法

font _id when ice extends 開發 content androi mark 關於消息的提示有兩種:一種是Toast,一種就是Notification。前者維持的時間比較短暫,後者維持的時間比較長。 並且我們尋常手機的應用比方網易、貼吧等等都有非常多

OGG維護優化腳本-OGG狀態監控系統--後臺腳本

oracle ogg goldengate 這個簡易監控系統具體是由html實現的後臺沒有數據庫,只有從各臺機器收集並上傳過來的html文件通過定時shell腳本整理並分類到各個目錄,然後通過apache被網頁調用具體更新頻率取決於各數據同步服務器的定時任務運行頻率OGG進程狀態整理腳本這個腳本用

聊聊高並發解析java.util.concurrent各個組件 深入理解AQS

sar 成功 通知 ati help write ng- ads 同步 近期總體過了下AQS的結構。也在網上看了一些講AQS的文章,大部分的文章都是泛泛而談。又一次看了下AQS的代碼,把一些新的要點拿出來說一說。 AQS是一個管程。提供了一個主要的同步器的

Python學習筆記StringIO和BytesIO

nbsp from 寫入 enc print == world! byte 初始化 StringIO 很多時候,數據讀寫不一定是文件,也可以在內存中讀寫。 StringIO顧名思義就是在內存中讀寫str。 要把str寫入StringIO,我們需要先創建一個String

笨辦法學Python

love pla ide nor simple open start close sce 習題 24: 更多練習 你離這本書第一部分的結尾已經不遠了,你應該已經具備了足夠的 Python 基礎知識,可以繼續學習一些編程的原理了,但你應該做更多的練習。這個練習的內容比

設計模式---狀態模式

inter man 每一個 con ret ride text src 定義 1、簡介   定義對象間的一種一對多的依賴關系,當一個對象的狀態發生改變時,所有依賴於它的對象都得到通知並被自動更新。   狀態模式允許一個對象在其內部狀態改變時改變其行為,這個對象看上去就像改變

C#編程----------修飾符

宋體 public ext str 繼承 應用 man 空間 layout 修飾符 修飾符即應用於類型或成員的關鍵字.修飾符可以指定方法的可見性,如public或private,還可以指定一項的本質,如剛發的vritual或abstract. 可見性的修飾符 修

Python學習—— 前端基礎之Bookstrap

asc script nta 學習 基本 www. red hicon 分享 一、Bootstrap介紹 Bootstrap是Twitter開源的基於HTML、CSS、JavaScript的前端框架。 它是為實現快速開發Web應用程序而設計的一套前端工具包。 它支持響應式布

企業分布式微服務雲SpringCloud SpringBoot mybatis 用spring Restdocs創建API文檔

str () 分布式 ava 顯示 網站發布 shu this 過程 Restdoc,通過單元測試生成api文檔 restdocs是通過單元測試生存snippets文件,然後snippets根據插件生成htm文檔的。 建一個單元測試類: @RunWith(SpringRu

JmeterJmeter-Question之“加密請求參數”

直接 接口 成功 void return beanshell 32位 clas 圖片   日常接口測試碰到參數加密的情況不在少數,當然與之相對的也有解密。直接記錄實例:   排除各家用的不一樣的加密方式,用的最多的還是MD5加密(16,32)。Jmeter3.2版本已經有解

Linux學習筆記管道符和作業控制、shell變量、環境變量配置文件

環境變量配置文件 管道符 一、管道符和作業控制cat 1.txt |wc -l ; cat 1.txt |grep ‘aaa‘將前面命令的結果輸入給後面的命令ctrl z 暫停一個任務、這時候任務隱藏到後臺,通過fg可以重新吊起任務到前臺運行jobs查看後臺的任務bg[id]把任務調到後臺fg[id]

大數據筆記——Scala面向對象編程實例

產生 一起 ble lec gen hello ide 在一起 err ===================== Scala語言的面向對象編程 ======================== 一、面向對象的基本概念:把數據和操作數據的方法放到一起,作為一個整體(類 cl

C之指針

C語言 指針 在 C 語言中用的最多的就是指針了,指針同樣也是 C 語言的精華所在了。我們今天就來看看指針到底是什麽,它是如何使用的。 我們先來回想下變量,在程序中的變量只是一段存儲空間的別名,那麽是不是必須要通過這個別名才能使用這段存儲空間呢?答案肯定不是的,我們還可以

python2.7練習小例子

[] sum output inf Coding == sys pop lse 24):1、題目:利用遞歸方法求5!。 程序分析:遞歸公式:fn=fn_1*4! #!/usr/bin/python # -*- coding: UTF-8 -*- de

python學習開發接口

接口 quest HERE cut eth 學習 ret false 數據庫 模擬一些接口,在接口沒有開發成功前,可以用它來測試 用來查詢數據 1、第一個接口 import flask,json #__name__,代表當前這個python文件 server=flask.

操作符重載的概念

C++ 操作符重載 全局函數重載 成員函數重載 我們上節學習了操作符重載,在最後我們提出了用 + 號來實現復數的相加,但是復數是不能直接相加的。那麽我們下來來嘗試下#include <stdio.h> class Complex { int a;

Java學習總結——前端:CSS樣式設計CSS引入,選擇器,盒子模型,浮動元素

引入 itl AI dropdown 正常的 type ID 總結 網頁 一.CSS引入方式1.CSS簡介:(1)CSS(Cascading style Sheets):層疊樣式表。用來給html網頁設置樣式;(2)當多個選擇器對同一個元素進行樣式設計時,則該元素的樣式為多

面向對象

函數 str true enc system 白色 創建 div r+ class Car { int num; String name; String color; public static void run() {