2
我学习Scala,并作为一个任务,我必须用折叠生成廉政局的名单:斯卡拉:使用折叠生成的列表诠释的
// Generate list of integers by applying f to b until it returns None
def unfold(b: Int, f: Int => Option[(Int, Int)]): IntList = {
f(b) match {
case None => Nil()
case Some((x, y)) => Cons(x,unfold(y, f))
}
}
此展开工作得很好,但现在我不得不写一个直接的,老实说,我完全失去了什么?所以我只是随机开始,但这根本不起作用。一些帮助将不胜感激!
// generate the list of integers from i until j
def fromUntil(i: Int, j: Int): IntList = {
unfold(i, (x: Int) =>if(x < j) Some((j, j - 1)) else None)
}
}
杰罗姆