5-1更多的表处理函数
更多的表函数 表方法(1) 子表和元素访问: 表方法(2) 建新表 查找元素 last方法的实现 head的时间复杂度是常量时间。 last的时间复杂度呢? 为了寻找答案,我们写一个单独的函数实现last。 last花费的步数与表的长度成正比例。 练习 像last一样,实现一个独立的函数init 实现表的连接 如何实现表的连接呢? 让我们写一个独立的函数: concat的时间复杂度是多少? 实现表的倒转 如何实现表的倒转呢? 让我们写一个独立的函数: reverse的时间复杂度是多少? 我们能做得更好吗? 练习 移除表xs的第n个元素。如果n超出表的边界,返回表自身xs。 使用示例: 练习(难,选做) 扁平化表 def last[T](xs: List[T]): T = xs match {case List() = throw new Error(“last of empty list”)case List(x) = xcase y :: ys = last(ys)} * def last[T](xs: List[T]): T = xs match {case List() = throw new Error(“last of empty list”)case List(x) = xcase y :: ys = last(ys)} *
原创力文档

文档评论(0)