《Two Dozen Short Lessons in Haskell》学习(十五)- Encapsulation — modules

简介:

 

《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 A Haskell module provides a way to

a share variables and functions between scripts

b hide some of the variables and functions that a script defines

c package collections of variables and functions to be used in other scripts

d all of the above

 

2 The export list in a module designates variables and functions that

a are defined in the module and redefined in other modules

b are defined in the module and will be accessible to other scripts

c are defined in other scripts and needed in the module

d are defined in other scripts and redefined in the module

 

3 An import specification in a script

a makes all the definitions in a module available in the script

b designates certain variables and functions in the script to be private

c makes some public definitions from another module available for use in the script

d specifies the importation parameters that apply in the script

 

4 In a numeric representation scheme based on radix b,

a numbers are denoted by sequences whose elements come from a set of b digits

b numbers are written backwards

c letters cannot be used to represent digits

d numbers larger than b cannot be represented

 

5 Horner’s formula

a computes the reverse of a sequence of digits

b takes too long to compute when n is bigger than 10

c expresses a sum of multiples of powers of a certain base as a nest of products and sums

d is too complicated to use in ordinary circumstances

 

=========================================================

=========================================================

1 d

Haskell也用模块来管理源代码,可以在不同的代码之间共享函数,隐藏一些变量和实现细节。

module ModuleName (function1, function2)

模块名必须是大写字母开头;

括号里是输出列表(export list),表示这些函数可以共享给其它程序可用,类似java和C#等语言中的public关键字。

不在输出列表中的函数都是private私有的,其它程序访问不到这些函数。

通常的约定,一个.hs文件就是一个模块,文件名称就是模块名称。

 

2 b

模块里定义了一些函数,并提供一个输出列表,在这个输出列表上的函数,表示它们可以被其它模块访问。

输出列表的定义方式就是把一些函数名称写在括号里面:

module DecimalNumerals (integerFromDecimalNumeral, decimalNumeralFromInteger)

    where

    -- 后面写该模块中的函数定义

 

3 c

import语句使另外一个模块中的几个函数在当前这段程序中可见,与java语言类似,例如:

import DecimalNumerals (integerFromDecimalNumeral, decimalNumeralFromInteger)

引入DecimalNumerals模块中的两个函数, integerFromDecimalNumeral 和 decimalNumeralFromInteger这两个函数可以在当前程序里调用了。

 

4 a

该书中关于大数计算的例子让人感觉不太好理解,如果能换个大众容易理解的例子就好了。

这里是一个关于b进制数的表达方法,这是的b是进制的基数,d0, d1, …, dn就是第0位、第1位、…、第n位上的数字,对于10进制数,当然这些d0, d1, …, dn数字的范围就是0-9,对于16进制,范围就是0-15。

image

 

5 c

书中的horner公式用到了foldr函数

horner b ds = foldr (multAdd b) 0 ds

multAdd b d s = d + b*s

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

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

相关文章
|
4月前
|
JavaScript 前端开发
ES modules: A cartoon deep-dive
ES modules: A cartoon deep-dive
32 1
|
6月前
codeforces 344B - Simple Molecules
题意就是给出3个原子的化学价,然后组成一个分子,要保证这个分子是稳定的,如果你还记得高中化学知识的话这个很容易理解,然后让你求出1-2 2-3 1-3 号原子之间有几条键, 这里我分别用ta tb tc 表示, 用数学的方法表示出来的话就是a = tc + tb; b = ta+tc; c = ta + tb;可能有多种情况,只要输出一种即可。
21 0
【Simulink】Three-Phase V-I Measurement 使用方法
【Simulink】Three-Phase V-I Measurement 使用方法
HDLBits(1)——Modules:Hierarchy(上)
目录 HDLBits——Modules:Hierarchy 问题19 Module 将信息连接到端口 By position By name 问题20 Connecting ports by position(Module pos) 问题21 Connecting ports by name(Module name) 问题22 Three modules(Module shift) 问题23 Modules and vectors(Module shift8) 问题24 Adder 1(Module add) 问题25 Adder 2(Module fadd) 问题26 Carry-selec
128 0
HDLBits(1)——Modules:Hierarchy(上)
HDLBits(1)——Modules:Hierarchy(中)
目录 HDLBits——Modules:Hierarchy 问题19 Module 将信息连接到端口 By position By name 问题20 Connecting ports by position(Module pos) 问题21 Connecting ports by name(Module name) 问题22 Three modules(Module shift) 问题23 Modules and vectors(Module shift8) 问题24 Adder 1(Module add) 问题25 Adder 2(Module fadd) 问题26 Carry-selec
79 0
HDLBits(1)——Modules:Hierarchy(中)
HDLBits(1)——Modules:Hierarchy(下)
目录 HDLBits——Modules:Hierarchy 问题19 Module 将信息连接到端口 By position By name 问题20 Connecting ports by position(Module pos) 问题21 Connecting ports by name(Module name) 问题22 Three modules(Module shift) 问题23 Modules and vectors(Module shift8) 问题24 Adder 1(Module add) 问题25 Adder 2(Module fadd) 问题26 Carry-selec
87 0
HDLBits(1)——Modules:Hierarchy(下)
|
C语言
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的Multi-Pass+VBO”示例代码【C语言版】
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的Multi-Pass+VBO”示例代码【C语言版】
123 0
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的Multi-Pass+VBO”示例代码【C语言版】
|
JavaScript 算法 编译器
TypeScript 里的 module 解析过程 - Module Resolution
TypeScript 里的 module 解析过程 - Module Resolution
227 0
|
SQL XML 缓存
《Orca: A Modular Query Optimizer Architecture for Big Data》
Orca: A Modular Query Optimizer Architecture for Big Data
《Orca: A Modular Query Optimizer Architecture for Big Data》
|
XML 分布式计算 算法
Orca: A Modular Query Optimizer Architecture for Big Data
在之前的几片paper笔记中,对最为主流的两套优化器框架进行了解读,包括bottom-up dynamic programming的search策略和基于Top-down memorization的search策略
315 0
Orca: A Modular Query Optimizer Architecture for Big Data