{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 前言\n", "在Scala中,List是一个高效的数据结构,尤其对于很多递归算法尤其重要。它可以容易在头部添加元素,也可以得到列表的尾部。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 列表的最后一个元素" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mlist1\u001b[0m: \u001b[32mList\u001b[0m[\u001b[32mInt\u001b[0m] = \u001b[33mList\u001b[0m(\u001b[32m1\u001b[0m, \u001b[32m1\u001b[0m, \u001b[32m2\u001b[0m, \u001b[32m3\u001b[0m, \u001b[32m5\u001b[0m, \u001b[32m8\u001b[0m)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "val list1 = List(1, 1, 2, 3, 5, 8)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mfunction \u001b[36mlastBuildin\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def lastBuildin[A](ls: List[A]): A = ls.last" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mfunction \u001b[36mlastRecursive\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def lastRecursive[A](ls: List[A]): A = ls match {\n", " case h :: Nil => h\n", " case _ :: tail => lastRecursive(tail)\n", " case _ => throw new NoSuchElementException\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres3\u001b[0m: \u001b[32mInt\u001b[0m = \u001b[32m8\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "lastBuildin(list1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres4\u001b[0m: \u001b[32mInt\u001b[0m = \u001b[32m8\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "lastRecursive(list1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 列表的倒数第二个元素" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mfunction \u001b[36mpenultimateBuildin\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def penultimateBuildin[A](ls: List[A]): A =\n", " if(ls.isEmpty) throw new NoSuchElementException\n", " else ls.init.last" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mfunction \u001b[36mpenultimateRecursive\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def penultimateRecursive[A](ls: List[A]): A = ls match {\n", " case h :: _ :: Nil => h\n", " case _ :: tail => penultimateRecursive(tail)\n", " case _ => throw new NoSuchElementException\n", "}" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres7\u001b[0m: \u001b[32mInt\u001b[0m = \u001b[32m5\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "penultimateBuildin(list1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres8\u001b[0m: \u001b[32mInt\u001b[0m = \u001b[32m5\u001b[0m" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "penultimateRecursive(list1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Scala 2.11", "language": "scala211", "name": "scala211" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala211", "pygments_lexer": "scala", "version": "2.11.6" } }, "nbformat": 4, "nbformat_minor": 0 }