{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 8. Declaring types and classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.1 Type declarations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "new name for an existing type\n", "```\n", "type String = [Char]\n", "```\n", "name of type MUST begin with upper-case letter" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "type Pos = (Int,Int)\n", "type Trans = Pos -> Pos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Type declarations cannot be recursive; so the following is not allowed:\n", "```\n", "type Tree = (Int,[Tree])\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Type declarations can be parameterized by other types:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10,10)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(\"John\",\"Alice\")" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "type Pair a = (a,a)\n", "(10,10) :: Pair Int\n", "(\"John\",\"Alice\") :: Pair [Char]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(\"John\",32),(\"Alice\",22)]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "type Assoc k v = [(k,v)]\n", "[(\"John\",32),(\"Alice\",22)] :: Assoc [Char] Int\n", "t = [(\"John\",[10,20,30]),(\"Alice\",[1,2,3])] :: Assoc [Char] [Int]" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,2,3]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "[10,20,30]" ] }, "metadata": {}, "output_type": "display_data" }, { "ename": "", "evalue": "", "header": "MessageHeader {mhIdentifiers = [\"2225d6d9-1410-4f4c-a2a0-95263c66842d\"], mhParentHeader = Just (MessageHeader {mhIdentifiers = [\"2225d6d9-1410-4f4c-a2a0-95263c66842d\"], mhParentHeader = Nothing, mhMetadata = Metadata (fromList [(\"recordTiming\",Bool False),(\"deletedCells\",Array [String \"b05ddc0f-4f44-4209-bec6-d54771f2949f\",String \"3d18a4cc-6565-4150-907f-0f96c34d5a7d\",String \"125cb3e9-33f6-484a-9488-378a34a87b22\",String \"6c5e86a2-aa17-44ec-8ce3-6a03c773d069\",String \"a0a57ff5-2b8e-4787-9568-49d66ce41147\",String \"447b3b5b-7118-4f03-86a4-e780fea55fa9\",String \"7c783d8f-9c81-4824-8b93-62041a4d9474\",String \"7966eab5-f954-4be7-a8c2-621f262e8a4f\",String \"731b288a-584a-44a6-a58f-91c2e7d7bdad\",String \"bec6fc59-ab43-4b68-9407-5dc16dcfdb79\",String \"5dbe1361-ec34-414d-b106-dad2c4fb7169\",String \"7d249656-bde3-4c3c-bdb6-a4ed488929a6\",String \"4fcbe5dc-d3ae-4dea-9f8d-14e904649482\",String \"91607fbf-2117-4735-96a0-c7b835aced74\",String \"40d8a595-8ed2-4d1e-8e5e-e024d571246d\",String \"9c2e168f-694a-4a70-af7f-2f1bead8b69c\",String \"2c10189c-ab34-431c-be56-63b0e15a67e9\",String \"11a02f12-9a91-46ef-8ad3-0f719def7956\",String \"70b996f0-0ce7-4d64-a560-e9f7e0240f33\",String \"859fb5be-1e56-4f5e-8e14-cd1be5bbaa6a\"]),(\"cellId\",String \"ad548616-701e-4582-b582-cb3f01c5a70a\")]), mhMessageId = UUID {uuidToString = \"f27cef04-ed56-4913-a76e-7e2a2ffcac32\"}, mhSessionId = UUID {uuidToString = \"2225d6d9-1410-4f4c-a2a0-95263c66842d\"}, mhUsername = \"\", mhMsgType = ExecuteRequestMessage, mhBuffers = []}), mhMetadata = Metadata (fromList []), mhMessageId = UUID {uuidToString = \"ed06d74f-5e86-4677-9539-2e07364b83bc\"}, mhSessionId = UUID {uuidToString = \"2225d6d9-1410-4f4c-a2a0-95263c66842d\"}, mhUsername = \"\", mhMsgType = ExecuteErrorMessage, mhBuffers = []}", "output_type": "error", "traceback": [ "Prelude.head: empty list" ] } ], "source": [ "find :: Eq k => k -> Assoc k v -> v\n", "find k t = head [v | (k',v) <- t, k' == k]\n", "\n", "find \"Alice\" t\n", "find \"John\" t\n", "find \"James\" t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.2 Data declarations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A completely new type can be defined using the data mechanism of Haskell\n", "```\n", "data Bool = False | True\n", "```\n", "Here, | is read as \"or\" and the values are \"constructors\"; constructors MUST begin with upper-case letters" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "