{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Haskell exercise sheet 2\n", "\n", "## Basic list processing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions on lists\n", "\n", "Implement some general functions on lists such that the following expressions evaluate to `True`. Some of them can be found in the Prelude. Of course, do not use them." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myLast ['a', 'b', 'c', 'd'] == 'd'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myElementAt ['a', 'b', 'c', 'd'] 1 == 'b'\n", "myElementAt ['a', 'b', 'c', 'd'] 2 == 'c'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate a list\n", "\n", "Use a list as an accumulator to build up the list by prepending elements." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myRange 0 3 == [0, 1, 2, 3]\n", "myRange 5 12 == [5, 6, 7, 8, 9, 10, 11, 12]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myDuplicate ['a', 'b', 'c'] == ['a', 'a', 'b', 'b', 'c', 'c']\n", "myDuplicate [1, 2, 3] == [1, 1, 2, 2, 3, 3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myMulticate ['a', 'b', 'c'] 2 == ['a', 'a', 'b', 'b', 'c', 'c']\n", "myMulticate [1, 2, 3] 3 == [1, 1, 1, 2, 2, 2, 3, 3, 3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myEven [5, 6, 7, 8, 9, 10, 11, 12] == [6, 8, 10, 12]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mySplitAt [5, 6, 7, 8, 9, 10, 11, 12] 3 = ([5, 6, 7], [8, 9, 10, 11, 12])\n", "mySplitAt ['a', 'b', 'c', 'd'] 2 == (['a', 'b'], ['c', 'd'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Query a list\n", "\n", "Consider the following list of numbers and write functions that answer a few questions about the numbers in that list. Do not use existing functions from the Prelude, except `head`, `tail` and `(:)`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "z = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How many numbers are greater than five?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the smallest number greater than 5?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the average value of the numbers that come after a 5?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Haskell", "language": "haskell", "name": "haskell" }, "language_info": { "codemirror_mode": "ihaskell", "file_extension": ".hs", "name": "haskell", "pygments_lexer": "Haskell", "version": "8.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }