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

阅读: 评论:0

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

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

第六章 Patterns of Computation — Composition, Folding, and Mapping

1 Suppose that post is a function that, given two letters, chooses the one that follows the other in the alphabet

(post ’x’ ’y’ is ’y’; post ’u’ ’p’ is ’u’). Then the formula foldr1 post string delivers

a the letter from string that comes earliest in the alphabet

b the letter from string that comes latest in the alphabet

c the first letter from string

d the last letter from string

2 Suppose that && is an operator that, given two Boolean values, delivers True if both are True and False otherwise.

Then the formula foldr1 (&&) [False, True, False, True, True] delivers the value

a True

b False

c Maybe

d Nothing — the formula doesn’t make sense

3 In the formula foldr1 f [a, b, c, d]

a a, b, c, and d must have the same type

b f must deliver a value of the same type as its arguments

c f must be a function that requires two arguments

d all of the above

4 If f is a function that requires two arguments, then foldr1 f is a function that requires

a no arguments

b one argument

c two arguments

d three arguments

5 The second argument of foldr1 must be

a a sequence

b a function

c a sequence of functions

d a function of sequences

6 If next is a function that, given a letter, delivers the next letter of the alphabet, then the mapping process in the

formula [next c | c <- "hal"] delivers the string

a "lah"

b "ibm"

c "alm"

d "lha"

7 The string "is "hot" now"

a has four quotation marks in it

b has exactly two spaces and two back-slashes

c has 12 characters, including exactly two spaces

d has 14 characters, including exactly two spaces

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

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

1 b

foldr1是个相当强大的内置函数,它可以在一个列表内的每两个元素之间都插入一个运算符。

如:foldr1 (+) [1..10]就是1+2+3+4+5+6+7+8+9+10,即返回55。

fold后面的字母r表示这个运算符是向右连接的(如何解释?)。

Haskell的强大在于这个列表不仅仅指数值,还可以把函数连接起来,如foldr1 (.) [f, g, h]就是(f . g . h),这里的f,g,h都是函数

用括号把一个运算符包起来,表示是一个函数,如(+)和(.)

如果写成foldr1 . [f, g, h],就会把中间的点解释成一个运算符,从而报错。

所以(+) 2 3也就是指2+3,前面这种表达式在LISP语言中常见。

foldr1 post "haskell"  == 's'

2 b

foldr1 (&&) [False, True, False, True, True]就是False && True && False && True &&  True,就是“与”运算,返回False

函数的中缀表示法:div 5 2 == 5 `div`2。注意那个反向的`符号。

3 d

foldr1 f [a, b, c, d]

[a, b, c, d]列表必须要保证里面的元素都是同一种类型,[1, “a”]会报错

函数f一定有两个参数,两个参数与返回值都是同一种类型,后面学过类型后,f的类型应该是a –> a –> a。以前面的&&为例,a && b等价于(&&) a b,注意&&要用括号引起来。

想看一个函数的类型,可以在ghci(或winghci)中,执行:t 命令,后面跟上函数名称即可,例如 :t foldr1

得到:foldr1 :: (a -> a -> a) -> [a] –> a

4 b

foldr1有几个参数与f是无关的,foldr1总是需要2个参数,第一参数是一个函数(没错,函数也可以做为参数),第二个参数是一个列表

foldr1 f需要1个参数

5 a

foldr1的第二个参数是一个列表,这个列表可以是数值,也可以是函数

如第一题讲的,foldr1 (+) [1,2,3,4]中第二个参数是数值的列表

foldr1 (.) [f, g, h]中第二个参数是函数的列表,f, g, h都是函数

6 b

next得到一个字母在字母表中的下一个字母

[next c | c <- "hal"]对于"hal"中的每一个字符c,都调用 next c 函数,得到"ibm"

7 c

字符串的表示方式与C语言基本上一样,在表示引号时需要用转义符,"is "hot" now",有2个空格、2个引号、8个字母,共12个字符

本文发布于:2024-02-03 01:50:46,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170689624747847.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Short   Dozen   Haskell   Lessons
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23