《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还是先看其它一些入门书籍吧,这本书配套着学学还是不错的。
第18章 Interactive Keyboard Input and Screen Output
IO类型在初学Haskell的时候是一个很难理解的概念,平常的编程语言中已经习惯了输入、输出语句,但在函数式编程中一切皆函数,一个确定的函数会得到确定的计算结果,而与操作系统交互时函数式编程就不太方便了,这时Haskell引出了一个IO类型。
一个重要的do表达式:
这一章还介绍了一个字符串连接的函数,可以把字符串末尾加上换行符,再连接起来
unlines :: [String] -> String
unlines = concat . map (++ "\n")
例如:
unlines ["line1", "line2", "line3"] = "line1\nline2\nline3\n"
1 Values of IO type
a are in the equality class Eq
b specify requests for operating system services
c represent tuples in a unique way
d describe Jovian satellites
2 Which of the following intrinsic functions in Haskell causes output to appear on the screen?
a concat :: [[any]] -> [any]
b putStr :: String -> IO ()
c printString :: Message –> Screen
d getLine :: IO String
3 What will be the effect of the command main, given the following script?
HASKELL DEFINITION •main =
HASKELL DEFINITION • do putStr "Good "
HASKELL DEFINITION • putStr "Vibrations\n"
HASKELL DEFINITION • putStr " by the Beach Boys\n"
a one line displayed on screen
b two lines displayed on screen
c three lines displayed on screen
d audio effects through the speaker
4 What will be the effect of the command main, given the following script?
HASKELL DEFINITION • main =
HASKELL DEFINITION • do putStr "Please enter your first and last name (e.g., John Doe): "
HASKELL DEFINITION • firstLast <- getLine
HASKELL DEFINITION • putStr (reverse firstLast)
a display of name entered, but with the last name first
b display of last name only, first name ignored
c display of last name only, spelled backwards
d display of name spelled backwards (书中在这里有印刷错误)
5 How should the last input/output directive in the preceding question be changed to display the first name only?
什么时候只输出名字?
a putStr(take 1 firstLast)
b putStr(drop 1 firstLast)
c putStr(takeWhile (/= ’ ’) firstLast)
d putStr(dropWhile (/= ’ ’) firstLast)
=========================================================
答
案
在
下
面
=========================================================
1 b
2 b
putStr函数返回的类型是IO (),表示要与操作系统有交互动作,这个()表示不返回任何数据。
3 b
结果应该是:
Good Vibrations
by the Beach Boys
4 d
如果输入是John Doe
则屏幕输出:eoD nhoJ
5 c
如果输入是John Doe
选项a:take 1 firstLast,会取出第一个字符,“J"
选项b:drop 1 firstLast,会除掉第一个字符, "ohn Doe"
选项c:takeWhile (/=' ') firstLast,会得到空格前的字符串,"John",是正确答案。
选项d:dropWhile (/=' ') firstLast,会除掉第一个空格前的所有字符," Doe",注意Doe前面还有一个空格
转载于:https://www.cnblogs.com/speeding/archive/2013/03/23/2976288.html
《Two Dozen Short Lessons in Haskell》学习(十八) - 交互式键盘输入和屏幕输出相关推荐
- 《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二十四学 ...
- 《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二十四学 ...
- 《Two Dozen Short Lessons in Haskell》学习(三)
<Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...
- 《Two Dozen Short Lessons in Haskell》学习(十六)- Definitions with Alternatives
<Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...
- 《Two Dozen Short Lessons in Haskell》学习(十三)迭代及重复的常规模式
<Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...
- 《Two Dozen Short Lessons in Haskell》(二十)分数
<Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...
- PyTorch框架学习十八——Layer Normalization、Instance Normalization、Group Normalization
PyTorch框架学习十八--Layer Normalization.Instance Normalization.Group Normalization 一.为什么要标准化? 二.BN.LN.IN. ...
- C1认证学习十八、十九(表单元素、转义字符)
C1认证学习十八.十九(表单元素.语义化标签) 十八 任务背景 HTML的表单用于收集用户的输入,表单元素是指的不同类型的input元素,复选框,单选按钮,提交按钮等等. 任务目标 掌握表单标签以及其 ...
- android学习十八(Service服务的基本用法)
定义一个服务 在项目中定义一个服务,新建一个ServiceTest项目,然后在这个项目中新增一个名为MyService的类,并让它继承自Service,完成后的代码如下所示: package com. ...
最新文章
- Git 2.25.0发布,支持部分clone、稀疏checkout
- Mongodb 请求处理流程
- 10.31NOIP模拟赛解题报告
- c语言编程 float p,C语言编程经典例题
- 【文末赠书】漫画:什么是 “跳表” ?
- Android(Fragment和Activity之间通信)
- java服务器修改缓存数据,java监控服务器数据存入缓存
- 虎扑入股“造物”App关联公司 后者为模玩、手办平台
- python随机函数random要导入哪个包_python学习之随机函数random
- LINUX 下mysql主从安装与同步
- C++设计模式--观察员
- altiumdesigner的基本你操作
- javaweb实现教师和教室管理系统 java jsp sqlserver
- thymeleaf 的hello world
- 值得尝试的 MQTT 客户端工具
- 彻底掌握基于HTTP网络层的 “前端性能优化“
- ngx_rtmp_module直播流程
- C++入门经典(第三版Ivor Horton著 ) 第一章习题答案
- 想要快乐陪伴左右吗?多种提高多巴胺的方法送给你
- python networkx 边权重_networkx(Python)中基于边权重排序的相邻边
热门文章
- [tensorflow、神经网络] - 使用tf和mnist训练一个识别手写数字模型,并测试
- Docker中未指定挂载点容器间volume卷的数据共享
- Vue 教程第九篇—— 动画和过度效果
- Android 布局学习之——Layout(布局)详解一
- Java Date Time 教程-时间测量
- Android开发学习笔记-自定义对话框
- 2013-8-14大一大二暑期组队训练赛
- 解密昇腾AI处理器--Ascend310简介
- 二层交换机、三层交换机和路由器的基本工作原理和三者之间的主要区别
- pthread_join/pthread_exit用法实例