《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还是先看其它一些入门书籍吧,这本书配套着学学还是不错的。

第十章 私有定义----where从句

Haskell中的许多名字也称为变量,像是数学方程中的变量variable一样。但与过程式编程完全不同,在函数式编程里这个量,当它定义了之后,实际并不改变。

where从句把数学中的局部定义的概念直接用在编程语言中了。

在SQL查询语句中where只是一个条件表达式,与haskell是不一样的。

1 Integral numbers are denoted in Haskell by

a decimal numerals enclosed in quotation marks

b decimal numerals — no quotation marks involved

c decimal numerals with or without quotation marks

d values of type String

2 The Haskell system, when interpreting scripts, represents numbers as

a decimal numerals

b binary numerals

c hexadecimal numerals

d however it likes — none of your business anyway

3 Encapsulation

a prevents name clashes

b prevents one software component from messing with the internal details of another

c is one of the most important concepts in software engineering

d all of the above

4 A where-clause in Haskell defines variables that

a will be accessible from all where-clauses

b take the name of the where-clause as a prefix

c cannot be accessed outside the definition containing the clause

d have strange names

5 In Haskell, the beginning and end of a definition is determined by

a begin and end statements

b matched sets of curly braces { }

c indentation

d however it likes — none of your business anyway

6 The intrinsic function foldr

a is more generally applicable than foldr1

b takes more arguments than foldr1

c can accommodate an empty sequence as its last argument

d all of the above

7 What value does the following command deliver?

HASKELL DEFINITION • ct xs= foldr addOne 0 xs

HASKELL DEFINITION •     where

HASKELL DEFINITION •     addOne x sum = 1 + sum

HASKELL COMMAND • ct [1, 2, 3, 4]

a 10, by computing 1+(2+(3+(4+0)))

b 4, by computing 1+(1+(1+(1+0)))

c 5, by computing 1+(1+(1+(1+1)))

d nothing — ct is not properly defined

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

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

1 b

5678是整数,而"5678”就是字符串,在各种编程语言都是这样。

2 d

Haskell内部如何表达整数,是haskell编译器或解释器的内部机制,不用关心,也没有指出过,。

3 d

封装可以防止名字冲突,在软件工程中常用的一个概念,可以防止与其它模块中的变量相混。

4 c

在where从句里的变量,当然通常是存在于一个函数定义内的,在这个函数定义内是可见的,但在其它地方都不可访问。

5 c

Haskell中的缩进是有含义的,当缩进时,表示前面的定义还没完。

当缩进回到了前面一个级别,则表示当前的定义结束了,开始一个新的定义。

6 d

foldr1函数有2个参数,而foldr有3个参数。

例如:foldr1 op [w,x,y] = w 'op' (x 'op' y)

对应于foldr op z [w,x,y] = w 'op' (x 'op' (y 'op' z))

并且foldr op z [] = []

foldr函数可以保证在空列表情况时,也可以得到返回值。而foldr1在给空列表时,出报错。

例如: foldr1 (+) []

报错:Exception: Prelude.foldr1: empty list

而foldr (+) 0 []会得到0

7 b

这个函数相当于统计元素的个数

《Two Dozen Short Lessons in Haskell》学习(一)Hello World

《Two Dozen Short Lessons in Haskell》学习(二)Definitions

《Two Dozen Short Lessons in Haskell》学习(三)How to Run Haskell Programs

《Two Dozen Short Lessons in Haskell》学习(四)List Comprehensions

《Two Dozen Short Lessons in Haskell》学习(五)Function Composition and Currying

《Two Dozen Short Lessons in Haskell》学习(六)Patterns of Computation – Composition, Folding, and Mapping

《Two Dozen Short Lessons in Haskell》学习(七)- Types

《Two Dozen Short Lessons in Haskell》学习(八)- Function Types, Classes, and Polymorphism

《Two Dozen Short Lessons in Haskell》学习(九)- Types of Curried Forms and Higher Order Functions

《Two Dozen Short Lessons in Haskell》学习(十)- Private Definitions — the where-clause

《Two Dozen Short Lessons in Haskell》学习(十一)- Tuples

《Two Dozen Short Lessons in Haskell》学习(十二) 数值相关的类

《Two Dozen Short Lessons in Haskell》学习(十三)迭代及重复的常规模式

《Two Dozen Short Lessons in Haskell》学习(十四)截断序列和惰性求值

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

《Two Dozen Short Lessons in Haskell》学习(十六)- Definitions with Alternatives

《Two Dozen Short Lessons in Haskell》学习(十七) - 模块库

《Two Dozen Short Lessons in Haskell》学习(十八) - 交互式键盘输入和屏幕输出

《Two Dozen Short Lessons in Haskell》学习(十九) - 文件输入与输出

《Two Dozen Short Lessons in Haskell》学习(二十)- 分数

《Two Dozen Short Lessons in Haskell》学习(二十一)- 在形式参数中使用模式匹配

《Two Dozen Short Lessons in Haskell》学习(二十二)- 递归

第23章没有习题。

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

转载于:https://www.cnblogs.com/speeding/archive/2012/12/08/2807844.html

《Two Dozen Short Lessons in Haskell》学习(十)- Private Definitions — the where-clause相关推荐

  1. 《Two Dozen Short Lessons in Haskell》学习(十六)- Definitions with Alternatives

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  2. 《Two Dozen Short Lessons in Haskell》学习(十八) - 交互式键盘输入和屏幕输出

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  3. 《Two Dozen Short Lessons in Haskell》学习(八)- Function Types, Classes, and Polymorphism

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  4. 《Two Dozen Short Lessons in Haskell》学习(十三)迭代及重复的常规模式

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  5. 《Two Dozen Short Lessons in Haskell》(二十)分数

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  6. 《Two Dozen Short Lessons in Haskell》学习(三)

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  7. Haskell学习笔记: type and typeclasses

    Haskell学习笔记:type and typeclasses Type 常见类型及注意事项 Type variables Typeclass 基本的typeclass 构造自己的type Reco ...

  8. 2017年深度学习十大趋势预测

    2017年深度学习十大趋势预测 本文作者曾经多次预测了技术发展的趋势,最近的一次预测是"2011年软件发展的趋势与预测".10项预言中,准确地命中了6项,比如JavaScript ...

  9. 强化学习(十九) AlphaGo Zero强化学习原理

    在强化学习(十八) 基于模拟的搜索与蒙特卡罗树搜索(MCTS)中,我们讨论了MCTS的原理和在棋类中的基本应用.这里我们在前一节MCTS的基础上,讨论下DeepMind的AlphaGo Zero强化学 ...

最新文章

  1. Android测试原理(二)
  2. python3 获取cpu 内存利用率
  3. python 命令行参数-Python 中最好用的命令行参数解析工具
  4. 和同学沟通,一定是时间效率比较高的
  5. TensorFlow 1.9开始支持树莓派
  6. Spring-Cloud中的统一配置中心
  7. 理解面向连接和无连接协议之间的区别
  8. 用quartus搭建soc-串口发送学号完整流程(软件+硬件)
  9. 明小子mysql_安全狗最新版SQL注入防护多种方式bypass(简简单单/各种数据库通用)...
  10. 日系PC厂商为问题希捷硬盘提供固件更新
  11. win10查看所有的wifi密码。
  12. Hello Qt(十六)——QT绘图实例-钟表
  13. CryEngine5官方 window下源码编译
  14. 单片机测量脉宽c语言程序,利用51系列单片机定时器功能实现测量脉冲宽度
  15. Dithering(Dithering pixel studio)
  16. Loadrunner11 录制手机App脚本多种方法介绍
  17. 持续信创| 骞云科技与东方通完成产品兼容性互认证
  18. 2010年水瓶座的运势
  19. Unity开发Excel表格读取器
  20. 北京java研发平均工资_各地java开发工程师平均工资 北京高级java开发工程师工资2万多...

热门文章

  1. Mac系统中MongoChef链接MongoDB集群的方法
  2. css --- 伸缩布局,让图片居中
  3. es6 --- Proxy实例的get方法
  4. es6 --- 内置的Symbol值
  5. 结构型模式--装饰模式
  6. 阴雨连绵潮湿加剧 车辆防潮提升保值
  7. 常用AT指令集 (转)
  8. 《鸟哥的linux私房菜-服务器篇 第三版》 RHCA亲授
  9. Linux Kconfig及Makefile学习
  10. “RuntimeWarning: overflow encountered in ubyte_scalars像素加减运算溢出异常”原因以及解决办法