A Read–Eval–Print Loop (REPL), also known as an interactive toplevel or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise. The term is most usually used to refer to programming interfaces similar to the classic Lisp machine interactive environment. Common examples include command line shells and similar environments for programming languages, and is particularly characteristic of scripting languages.[1]

In a REPL, the user enters one or more expressions (rather than an entire compilation unit) and the REPL evaluates them and displays the results. The name read–eval–print loop comes from the names of the Lisp primitive functions which implement this functionality:

The read function accepts an expression from the user, and parses it into a data structure in memory. For instance, the user may enter the s-expression (+ 1 2 3), which is parsed into a linked list containing four data elements.
The eval function takes this internal data structure and evaluates it. In Lisp, evaluating an s-expression beginning with the name of a function means calling that function on the arguments that make up the rest of the expression. So the function + is called on the arguments 1 2 3, yielding the result 6.
The print function takes the result yielded by eval, and prints it out to the user. If it is a complex expression, it may be pretty-printed to make it easier to understand. In this example, though, the number 6 does not need much formatting to print.
The development environment then returns to the read state, creating a loop, which terminates when the program is closed.

REPL — 交互式解释器环境。
R(read)、E(evaluate)、P(print)、L(loop)
输入值,交互式解释器会读取输入内容并对它求值,再返回结果,并重复此过程。

1 简介

近期对Scala比较感兴趣,买了本《快学Scala》,感觉不错。比《Programming Scala:Tackle Multi-Core Complexity on the Java Virtual Machine》好很多。 是本不错的入门书。而且每个章节都设置了难度级别,每章有习题,可以巩固Scala语法。

本文的目的就是针对这些习题进行解答

2 基础

 2.1 在Scala REPL中键入3,然后按Tab键。有哪些方法可以被应用?

这个。。。。直接操作一遍就有结果了.此题不知是翻译的问题,还是原题的问题,在Scala REPL中需要按3. 然后按Tab才会提示。 直接按3加Tab是没有提示的。下面是结果

!=             ##             %              &              *              +
- / < << <= ==
> >= >> >>> ^ asInstanceOf
equals getClass hashCode isInstanceOf toByte toChar
toDouble toFloat toInt toLong toShort toString
unary_+ unary_- unary_~ |

列出的方法并不全,需要查询全部方法还是需要到Scaladoc中的Int,Double,RichInt,RichDouble等类中去查看。

2.2 在Scala REPL中,计算3的平方根,然后再对该值求平方。现在,这个结果与3相差多少?(提示:res变量是你的朋友)

依次进行计算即可

scala> scala.math.sqrt(3)
warning: there were 1 deprecation warnings; re-run with -deprecation for details
res5: Double = 1.7320508075688772 scala> res5*res5
res6: Double = 2.9999999999999996 scala> 3 - res6
res7: Double = 4.440892098500626E-16

2.3 res变量是val还是var?

val是不可变的,而var是可变的,只需要给res变量重新赋值就可以检测res是val还是var了

scala> res9 = 3
<console>:8: error: reassignment to val
res9 = 3
^ 2.4 Scala允许你用数字去乘字符串—去REPL中试一下"crazy"*3。这个操作做什么?在Scaladoc中如何找到这个操作?
scala> "crazy"*3
res11: String = crazycrazycrazy

从代码可以推断,*是"crazy"这个字符串所具有的方法,但是Java中的String可没这个方法,很明显。此方法在StringOps中。

2.5 10 max 2的含义是什么?max方法定义在哪个类中?
直接在REPL中执行

scala> 10 max 2
res0: Int = 10 scala> 7 max 8
res1: Int = 8 scala> 0 max 0
res2: Int = 0

可以看出,此方法返回两个数字中较大的那个。此方法Java中不存在,所以在RichInt中。

2.6 用BigInt计算2的1024次方
简单的API调用

scala> BigInt(2).pow(1024)
res4: scala.math.BigInt = 1797693134862315907729305190789024733617976978942306572734300811577326758055009631327084773224
075360211201138798713933576587897688144166224928474306394741243777678934248654852763022196012460941194530829520850057688
38150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

2.7 为了在使用probablePrime(100,Random)获取随机素数时不在probablePrime和Radom之前使用任何限定符,你需要引入什么?

so easy. import需要的包啊。Random在scala.util中,而probablePrime是BigInt中的方法,引入即可

import scala.math.BigInt._
import scala.util.Random probablePrime(3,Random)

2.8 创建随机文件的方式之一是生成一个随机的BigInt,然后将它转换成三十六进制,输出类似"qsnvbevtomcj38o06kul"这样的字符串。查阅Scaladoc,找到在Scala中实现该逻辑的办法。

到BigInt中查找方法。

scala> scala.math.BigInt(scala.util.Random.nextInt).toString(36)
res21: String = utydx

2.9 在Scala中如何获取字符串的首字符和尾字符?

//获取首字符
"Hello"(0)
"Hello".take(1)
//获取尾字符
"Hello".reverse(0)
"Hello".takeRight(1)

2.10 take,drop,takeRight和dropRight这些字符串函数是做什么用的?和substring相比,他们的优点和缺点都是哪些?

查询API即可 take是从字符串首开始获取字符串,drop是从字符串首开始去除字符串。 takeRight和dropRight是从字符串尾开始操作。 这四个方法都是单方向的。 如果我想要字符串中间的子字符串,那么需要同时调用drop和dropRight,或者使用substring

http://www.ivanpig.com/blog/?p=452

快学Scala习题解答—第一章 基础的更多相关文章

  1. 快学Scala习题解答—第四章 映射和元组

    4 映射和元组  4.1 设置一个映射,当中包括你想要的一些装备,以及它们的价格.然后构建还有一个映射.採用同一组键,可是价格上打9折 映射的简单操作  ,"gun"->18 ...

  2. 快学scala习题解答--第五章 类

    5 类  5.1 改进5.1节的Counter类,让它不要在Int.MaxValue时变成负数 class Count{ private var value = Int.MaxValue else v ...

  3. 快学Scala习题解答—第三章 数组相关操作

    3 数组相关操作  3.1 编写一段代码.将a设置为一个n个随机整数的数组,要求随机数介于0(包括)和n(不包括)之间  random和yield的使用 import scala.math.rando ...

  4. 快学Scala习题解答—第十章 特质

    10 特质 10.1 java.awt.Rectangle类有两个非常实用的方法translate和grow,但可惜的是像java.awt.geom.Ellipse2D这种类没有. 在Scala中,你 ...

  5. 《快学Scala》第六章 对象 第七章 包和引入

  6. 《快学Scala》第五章 类

    关于case class和普通class的区别,可以参考: https://www.iteblog.com/archives/1508.html

  7. 《快学Scala》第四章 映射与元组

  8. 《快学Scala》第三章 数组相关操作

  9. 《快学Scala》

    Robert Peng's Blog - https://mr-dai.github.io/ <快学Scala>Intro与第1章 - https://mr-dai.github.io/S ...

随机推荐

  1. css3投影讲解、投影

    迷茫了好一段时间,今天开始整理一下自己,同时也整理下新的知识. CSS3: 从头开始做起:现在在页面中用到最多的是图片/容器投影,文字投影: 接下来就总结一个投影问题: box-shadow:阴影类型 ...

  2. PHP扩展开发之PHP的启动与终止

    PHP程序的启动可以看做是两个概念上的启动,终止也有两个概念上的终止.其中一个是PHP作为Apache(拿它举例,板砖勿扔)的一个模块的启动与终止, 这次启动php会初始化一些必要数据,比如与宿主Ap ...

  3. Lake Counting--poj2386

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23950   Accepted: 12099 D ...

  4. placeholder在不同浏览器下的表现及兼容方法 placeholder兼容

    1.什么是placeholder?    placeholder是html5新增的一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点(或 ...

  5. Oracle EBS-SQL (OM-6):打开订单.sql

    /*打开头*/ update oe_order_headers_all t set t.flow_status_code = 'BOOKED', t.open_flag = 'Y' where t.o ...

  6. Eclipse图标含义

    学习了这么久,之前也没注意,这次在csdn找个了文章,记录一下: Eclipse的Package Explorer中用图标表示了很多内容,刚刚开始接触Eclipse时对这些图标表示的内容并不清楚,而且 ...

  7. Delphi通过GetFileVersionInfo和VerQueryValue等API函数取得详细EXE信息

    This has been described at About: http://delphi.about.com/cs/adptips2001/a/bltip0701_4.htmBasically, ...

  8. spark快速入门之最简配置 spark 1.5.2 hadoop 2.7 配置

    配置的伪分布式,ubuntu14.04上 先配置hadoop,参见这个博客,讲的很好 http://www.powerxing.com/install-hadoop/, 但是我在配的过程中还是遇到了问 ...

  9. CURL 宏定义列表

    摘自http://blog.csdn.net/msda/article/details/38047809/ CURL 宏定义列表 列表CURL库一共有17个函数 curl_close:关闭CURL会话 ...

  10. subllime text 创建可复用的代码片段

    对于前端工程师来讲,写一个html页面的基本结构是体力活,每次去拷贝一个也麻烦,sublime text 2 提供了一个很好的复用代码片段.下面介绍一下创建一个html5的代码片段的过程. 在菜单上点 ...