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

《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中的缩进是有含义的,当缩进时,表示前面的定义还没完。

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

image

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274450.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

我和掘金合作的源码共读小册报名快1000人了~

众所周知,我和掘金合作出了源码共读第一期。我是前端领读员。现在报名快1000人了~奖品丰厚。也有一些小伙伴已经写了好几期笔记了~但相对1000人写的还是太少。什么?你不知道?那也很正常,毕竟我的公众号常读人数比较少。大部分人都…

【短语学习】盈余量分析(earned value analysis)

作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 各种形式的盈余量分析是衡量执行时最常用的方法。它把范围、成本和进度等度量标准结合在一起以帮助项目管理小组评估项目执行。对每项活动而言,盈余量分析包括计算三个主要数值:…

配音剧本_网络的各个阶段:剧本如何传达更好的UX

配音剧本让我们将焦点放在使用剧本技巧提升显微镜上。 (Let’s put the spotlight on elevating microcopy with playwriting techniques.) “Anything you put in a play — any speech — has got to do one of two things: either define character or push the action of t…

极致编译速度,一文搞定webpack5升级

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 lxchuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

全库模式 用户模式 表模式_暗模式,亮模式和用户的故事

全库模式 用户模式 表模式I have been working on designing a UI for an app that has individuals over the age of 60 as its main audience. At some point, I found my design more appealing in dark mode. As a UX designer, I know that my opinions and preferences d…

Rollup 与 Webpack 的 Tree-shaking

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 lxchuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

聚类与分类的主要区别在于:_经验在于细节:分析流服务的用户体验

聚类与分类的主要区别在于:看不见的差异 (The Invisible Difference) When app markets mature the overlap in features and designs grows closer as they catch up and copy each other. The more similar the apps are to one another, the more important the …

asp.net 动态创建TextBox控件 如何加载状态信息

接着上文Asp.net TextBox的TextChanged事件你真的清楚吗? 这里我们来说说状态数据时如何加载的。虽然在Control中有调用状态转存的方法,但是这里有一个判断条件 if (_controlState > ControlState.ViewStateLoaded) 一般的get请求这里的条件是不满足…

从零实现一个迷你 Webpack

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 lxchuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

ios 刷新遮罩遮罩_在Adobe XD中进行遮罩的3种方法

ios 刷新遮罩遮罩Are you new to Adobe XD? Or maybe you’re just stuck on how to create a simple mask? Here are 3 quick tips for how to mask your photos and designs in Adobe XD.您是Adobe XD的新手吗? 或者,也许您只是停留在如何创建简单的…

Vite 4.0 正式发布!

源码共读我新出了:第40期 | vite 是如何解析用户配置的 .env 的链接:https://www.yuque.com/ruochuan12/notice/p40也可以点击文末阅读原文查看,欢迎学习记笔记~12 月 9 日,Vite 4.0 正式发布。下面就来看看 Vite 4.0 有哪些更新吧…

图像标注技巧_保护互联网上图像的一个简单技巧

图像标注技巧补习 (TUTORIAL) Have you ever worried about sharing your images on the Internet? Anytime you upload something to the web you risk the chance of your work being used (without permission) by another.您是否曾经担心过要在Internet上共享图像&#xf…

【VueConf 2022】尤雨溪:Vue的进化历程

大家好,我是若川。我持续组织了近一年的源码共读活动,感兴趣的可以 点此扫码加我微信 lxchuan12 参与,每周大家一起学习200行左右的源码,共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

WCF netTcpBinding寄宿到IIS7

config配置文件不多说 <?xml version"1.0" encoding"utf-8" ?> <configuration><system.serviceModel><behaviors><serviceBehaviors><behavior name"myBehavior"><serviceMetadata/></behavior…

ar软件测试工具_如何为用户测试制作快速的AR原型

ar软件测试工具We had a project recently with an element of AR-based interaction, which it turned out was impossible to create as a prototype in either Invision or Framer (our current stack). This had a massive impact on our ability to test with users in a …

未来ui设计的发展趋势_2025年的未来UI趋势?

未来ui设计的发展趋势Humans are restless.人类是不安的。 Once we find something that works, we get used to it and we crave the next big thing. The next innovation. When will the future finally arrive? And when it does, how long will it take us to get used …

Monorepo 在网易的工程改造实践

大家好&#xff0c;我是若川。我持续组织了近一年的源码共读活动&#xff0c;感兴趣的可以 点此扫码加我微信 lxchuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

这一年,Vue.js 生态开源之旅带给我很大收获~

大家好&#xff0c;我是若川。我持续组织了近一年的源码共读活动&#xff0c;感兴趣的可以 点此扫码加我微信 lxchuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…

CSSyphus:烦躁不安的烦恼设计指南。

I’m trapped at home with my website. Or maybe it’s trapped at home with me. While some are using the weird lump of time provided by lockdown to indulge in baking, dancing, painting, singing, I’m using it to play around with code.我 被自己的网站困在家里。…

你构建的代码为什么这么大?如何优化~

大家好&#xff0c;我是若川。我持续组织了近一年的源码共读活动&#xff0c;感兴趣的可以 点此扫码加我微信 lxchuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试…