当前位置: 首页 > news >正文

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

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

第四章 Computations on Sequences — List Comprehensions

 

1 The following function delivers

HASKELL DEFINITION • f str = [ c | c <- str, c == ’x’ ]

a all the c’s from its argument

b an empty string unless its argument has x’s in it

c a string like its argument, but with x’s in place of c’s

d nothing — it contains a type mismatch, so it has no meaning in Haskell

 

2 The following command delivers

HASKELL DEFINITION • g str = [ c | c <- str, c == "x" ]

HASKELL COMMAND • g "xerox copy"

a "c"

b "xx"

c "xerox xopy"

d error — g expects its argument to be a sequence of strings, not a sequence of characters

 

3 The following function delivers a string like its argument, but …

HASKELL DEFINITION • h str = [ c | c <- reverse str, c < ’n’ ]

a written backwards if it starts with a letter in the first half of the alphabet

b written backwards and without n’s

c written backwards and without letters in the first half of the alphabet

d written backwards and without letters in the last half of the alphabet

 

4 Which of the following equations defines a function that delivers a string like its second argument, but with no letters preceding, alphabetically, the letter specified by its first argument?

下面哪个函数得到一个字符串,像第二个参数str,但没有小于某个字母(由第一个参数所指定)的字符?

A HASKELL DEFINITION • s x str = [ c | c <- str, c < x ]

B HASKELL DEFINITION • s x str = [ c | c <- str, c >= x ]

C HASKELL DEFINITION • s abc str = [ c | c <- str, c == "abc"]

D HASKELL DEFINITION • s abc str = [ c | c <- str, c /= "abc"]

 

5 In the following definition, the parameter str

HASKELL DEFINITION • f str = [ c | c <- str, c == ’x’ ]

a represents the letter x

b represents the letter c

c stands for a sequence of x’s

d stands for a string containing a sequence of characters

 

 

答案:

1 b

f str = [ c | c <- str, c == ’x’ ]

这里f是函数名称,str是参数

list Comprehensions应该是haskell里强大的特性了,[ 左边 | 右边 ],在竖线的左边是一个表达式(感觉叫表达式不太准确),右侧表示参数的取值范围和条件,右侧可以是生成器generator,像v <- expr这样,也可以是一个布尔过滤条件guard,像c<x这样。

Haskell具有数学的美感,从这个列表[2*x | x<-N, x<=10]与下面这个数学公式的相似度可以看出。

2012-11-07 16-24-27

 

2 d

这个题有一定的迷惑性,注意c == "x",这里是一个字符串(而不是字符),只有一个字母x。

'x’与"x”是两种不同类型的值,前面是Char,后面的是[Char]或称为String

所以调用g "xerox copy"就会报错

如果题目改为g ["xerox copy"]就不会报错了,由于"xerox copy"不等于"x”,所以这时的最终结果将是一个空串[]

 

3 d

'n’是26个字母中的第14个字母,c < ’n’ 就是指字母表的前半部分,也就是没有后半部分字母。

 

4 b

注意Haskell里判断不等于是用“/=”,而不是C语言中的“!=”,其它==, >, <, >=, <=都与C语言一样。

s x str = [ c | c <- str, c >= x ]这个函数定义中有2个参数,意思就是把str字符串中的所有大于或等于x的字母全筛选出来

执行s 'o' "Two Dozen Short Lessons in Haskell"的结果就是"woozortssoss"

 

5 d

f str = [ c | c <- str, c == ’x’ ]

这里的str是一个形式参数,它的类型是根据后面的表达式推断出来的

[ c | c <- str, c == ’x’ ]是一个列表,而列表中的元素是字符,所以str的类型就是[Char],也就是String

 

《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》(二十四)代数类型

 

相关文章:

  • 兰亭集势笔试题
  • netty vs mina netty和mina的区别
  • 便签
  • vcenter Converter 转换linux服务器报错
  • LINQ to SQL 建立实体类
  • WinCE6.0下在Static Text控件中显示JPG图片
  • no x11 display variable was set but this progra...
  • Android开发视频教学第一季(1-16集)视频源码下载
  • 机柜就是数据中心
  • 信息防泄露:如何为企业数据设一道安全关卡?
  • LINQ学习第一天
  • VS2012下使用 LocalDB
  • 100-47
  • .Net Winform开发笔记(一)
  • 读《实战nginx-取代Apache的高性能web服务器》-Nginx HTTP 负载均衡和反向代理的配置与优化精华纪要...
  • 【EOS】Cleos基础
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • iOS | NSProxy
  • java B2B2C 源码多租户电子商城系统-Kafka基本使用介绍
  • Java知识点总结(JavaIO-打印流)
  • yii2权限控制rbac之rule详细讲解
  • 翻译:Hystrix - How To Use
  • 好的网址,关于.net 4.0 ,vs 2010
  • 后端_ThinkPHP5
  • 使用parted解决大于2T的磁盘分区
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 协程
  • 你对linux中grep命令知道多少?
  • 1.Ext JS 建立web开发工程
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • ​ubuntu下安装kvm虚拟机
  • # Maven错误Error executing Maven
  • # Panda3d 碰撞检测系统介绍
  • #1014 : Trie树
  • #每天一道面试题# 什么是MySQL的回表查询
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (附源码)计算机毕业设计高校学生选课系统
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • .net core 控制台应用程序读取配置文件app.config
  • .net 程序发生了一个不可捕获的异常
  • /bin/rm: 参数列表过长"的解决办法
  • /usr/bin/env: node: No such file or directory
  • [ 数据结构 - C++]红黑树RBTree
  • [3D基础]理解计算机3D图形学中的坐标系变换
  • [ARM]ldr 和 adr 伪指令的区别
  • [BUUCTF NewStarCTF 2023 公开赛道] week3 crypto/pwn
  • [C# 开发技巧]如何使不符合要求的元素等于离它最近的一个元素
  • [C#]OpenCvSharp使用帧差法或者三帧差法检测移动物体
  • [C#小技巧]如何捕捉上升沿和下降沿
  • [C++参考]拷贝构造函数的参数必须是引用类型
  • [CLR via C#]11. 事件
  • [codeforces] 25E Test || hash
  • [Design Pattern] 工厂方法模式