快学Scala课后习题答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

快学Scala课后习题答案
分享⼀个之前做快学Scala的课后习题(2-21章节,19⽆)的Github链接,我把习题的⽂字写在了每个回答的注释上⾯,这样⽅便⼤家对照着看,省的回过头去对照着pdf看了,如果有做的不对的地⽅希望⼤家给予指正。

举⼀个第⼆章节的例⼦,抛砖引⽟:
object charpter02 {
/*
* 2.1
* ⼀个数字如果为正数,则它的signum为1;
* 如果是负数,则signum为-1;
* 如果为0,则signum为0.编写⼀个函数来计算这个值
* */
def signum(x: Int): Int = {
if (x > 0) { 1 }
else if (x < 0) { -1 }
else { 0 }
}
def signum(x: Int): Int = if (x > 0) 1 else if (x < 0) -1 else 0;
def signum(x: Int) = { if (x > 0) 1 else if (x < 0) -1 else 0 }
def signum(x: Int) = if (x > 0) 1 else if (x < 0) -1 else 0
/*
* 2.2
* ⼀个空的块表达式{}的值是什么?类型是什么?
* */
// def checkEmptyBlockType() = {
// println({});
// println({}.getClass())
// }
def checkEmptyBlockType { println({}); println({}.getClass()) }
/*
* 2.3
* 指出在Scala中何种情况下赋值语句x=y=1是合法的。

* (提⽰:给x找个合适的类型定义)
*/
def checkAssignLegal {
var x: Unit = ()
println("x's type is: " + x.getClass)
var y = 1
x = y = 1
}
/*
* 2.4
* 针对下列Java循环编写⼀个Scala版本:
* for(int i=10;i>=0;i–) System.out.println(i);
*/
def ScalaForeach {
// 1.to(10).reverse.foreach { (i: Int) => Predef.println(i) }
// 1.to(10).reverse.foreach { i => Predef println i }
// 1.to(10).reverse.foreach { i => println(i) }
// 1.to(10).reverse foreach { println _ }
(1 to 10 reverse) foreach println
}
/*
* 2.5
* 编写⼀个过程countdown(n:Int),打印从n到0的数字
*/
def countdown(n: Int) {
n match {
case n if n >= 0 => {
(0 to n reverse) foreach println
}
case n if n < 0 => {
n to 0 foreach println
}
}
}
/*
* 2.6
* 编写⼀个for循环,计算字符串中所有字母的Unicode代码的乘积。

* 举例来说,"Hello"中所有字符串的乘积为9415087488L
*/
def calculateCharsUnicodeProduct(s: String) = {
var res: Long = 1
s foreach { res *= _.toLong }
res
}
/*
* 2.7
* 同样是解决前⼀个练习的问题,但这次不使⽤循环。

* (提⽰:在Scaladoc中查看StringOps)
*/
// def computeCharsUnicodeProduct(s: String) = (1.toLong /: s) { _ * _ }
def computeCharsUnicodeProduct(s: String) = s.foldLeft(1.toLong) { _ * _ } /*
* 2.8
* 编写⼀个函数product(s:String),
* 计算前⾯练习中提到的乘积
* 2.9
* 把前⼀个练习中的函数改成递归函数
*/
// def product(s: String) = {
// if (s.length() == 1) s(0) toLong
// else s(0).toLong * product(s.tail)
// }
def product(s: String): Long = {
if (s.length() == 1) s(0) toLong
else s(0).toLong * product(s.tail)
}
/*
* 2.10
* 编写函数计算xn,其中n是整数,使⽤如下的递归定义:
*/
def question10(x: Int, n: Int): BigInt = n match {
case 0 => 1
case n if n < 0 => 1 / question10(x, -n)
case n if n % 2 == 0 => question10(x, n / 2) pow 2
case n if n % 2 == 1 => x * question10(x, n - 1)
}
def main(args: Array[String]): Unit = {
// checkEmptyBlockType()
}
}。

相关文档
最新文档