{"id":208,"date":"2022-05-29T21:03:13","date_gmt":"2022-05-29T21:03:13","guid":{"rendered":"https:\/\/kindsonthegenius.com\/elm\/?p=208"},"modified":"2022-05-29T21:03:13","modified_gmt":"2022-05-29T21:03:13","slug":"elm-custom-types-algebraic-data-types","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/","title":{"rendered":"Elm &#8211; Custom Types (Algebraic Data Types)"},"content":{"rendered":"<p>In this tutorial, you will learn how to create custom types in Elm. We would focus on algebraic data types and how it is implemented in Elm.<\/p>\n<ol>\n<li><a href=\"#t1\">Algebraic Data Types\/Union Types<\/a><\/li>\n<li><a href=\"#t2\">Type Constructor<\/a><\/li>\n<li><a href=\"#t3\">Type Annotations<\/a><\/li>\n<li><a href=\"#t4\">Recursive Types<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Algebraic Data Types\/Union Types<\/strong><\/h4>\n<p>An algebraic data type (ADT) is a type that is composed of other types. This allows us to define a type and specify all the instances\u00a0the type can assume.<\/p>\n<p>In the code below, we create a type called Greeting which can take either one of four variants:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">type<\/span> <span style=\"color: #333399; font-weight: bold;\">Employee<\/span> <span style=\"color: #333333;\">=<\/span>\r\n      <span style=\"color: #bb0066; font-weight: bold;\">Doctor<\/span> <span style=\"color: #bb0066; font-weight: bold;\">String<\/span> <span style=\"color: #bb0066; font-weight: bold;\">String<\/span>\r\n    <span style=\"color: #333333;\">|<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Driver<\/span> <span style=\"color: #bb0066; font-weight: bold;\">String<\/span> <span style=\"color: #bb0066; font-weight: bold;\">String<\/span>\r\n    <span style=\"color: #333333;\">|<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Intern<\/span> <span style=\"color: #bb0066; font-weight: bold;\">String<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Int<\/span>\r\n    <span style=\"color: #333333;\">|<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Janitor<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The first three variants: Doctor, Driver and Intern are called <em>data constructors<\/em>. This is because they can be considered as a constructor with parameters. The last variant Janitor is a value by itself. It is also called a nullary data constructor, that is, a constructor that takes no arguments.<\/p>\n<p>The Employee type we created in the previous section is called an <em>algebraic data type<\/em> or a <em>union type<\/em> or <em>tagged unions<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Type Constructor<\/strong><\/h4>\n<p>Maybe is a built-in type in Elm that allows you to model the idea of possible non-existent value. Sometimes, we are not sure whether a value is returned. For instance when converting a String to Int. The convert could either succeed and return a valid value or It could also fail if the input string is could not be converted.<\/p>\n<p>The type definition for Maybe is<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007020;\">type<\/span> Maybe a\r\n    <span style=\"color: #333333;\">=<\/span> Just a\r\n    <span style=\"color: #333333;\">|<\/span> Nothing\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Another example would be attempting to 11th element of an 10 element array. In this case, instead of returning an error, we return Nothing.<\/p>\n<p>To create a value of type Maybe, we could either use the Just data constructor or the Nothing constant. Notice that in the type definition for Maybe, we have a value a. This is a type variable that represents a\u00a0 an argument to a type constructor.<\/p>\n<p>We can also create our own generic type simply by passing an argument to the type constructor. In the code below, we modify the Employee type so that it accepts a type argument.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007020;\">type<\/span> Employee a <span style=\"color: #333333;\">=<\/span>\r\n      Doctor String String\r\n    <span style=\"color: #333333;\">|<\/span> Driver String String\r\n    <span style=\"color: #333333;\">|<\/span> Intern a\r\n    <span style=\"color: #333333;\">|<\/span> Janitor\r\n<\/pre>\n<p>So now that we can pass an argument to Employee, the Intern data constructor can take any type.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Type Annotation ( filterMap Function)<\/strong><\/h4>\n<p>Functions and values in Elm can be annotated with type annotations. They tell us how the function work. For example, if you type <em>List.filterMap<\/em> in the Elm <em>Repl<\/em>, you can see the type annotation as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #bb0066; font-weight: bold;\">List<\/span><span style=\"color: #333333;\">.<\/span>filterMap\r\n<span style=\"color: #333333;\">&lt;<\/span>function<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">a<\/span> <span style=\"color: #333399; font-weight: bold;\">-&gt;<\/span> <span style=\"color: #333399; font-weight: bold;\">Maybe<\/span> <span style=\"color: #333399; font-weight: bold;\">b<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333399; font-weight: bold;\">-&gt;<\/span> <span style=\"color: #333399; font-weight: bold;\">List<\/span> <span style=\"color: #333399; font-weight: bold;\">a<\/span> <span style=\"color: #333399; font-weight: bold;\">-&gt;<\/span> <span style=\"color: #333399; font-weight: bold;\">List<\/span> <span style=\"color: #333399; font-weight: bold;\">b<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The type annotation tells us that the <em>filterMap<\/em> function takes two arguments:<\/p>\n<ul>\n<li>a function that takes a value and returns a Maybe b<\/li>\n<li>a list of values of type a<\/li>\n<\/ul>\n<p>Finally, the <em>filterMap<\/em> function returns a list of values of type b. The <em>filterMap<\/em>\u00a0 applies the function to each element of <em>List<\/em> a keeps the result when a value is returned.<\/p>\n<p>You can try it with the example below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> List<span style=\"color: #333333;\">.<\/span>filterMap (\\x <span style=\"color: #333333;\">-&gt;<\/span> toInt x)  [<span style=\"background-color: #fff0f0;\">\"2\"<\/span>, <span style=\"background-color: #fff0f0;\">\"3s\"<\/span>, <span style=\"background-color: #fff0f0;\">\"4\"<\/span>]\r\n<\/pre>\n<p>This would output the result <strong>[2, 4]<\/strong><\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Recursive Types<\/strong><\/h4>\n<p>One typical recursive type in Elm is a list. This means that a list of values is build recursively with an empty list as the base. So if we have the list [2, 13, 9], it is actually constructed in the following steps:<\/p>\n<ul>\n<li>[]<\/li>\n<li>9 :: [] = [9]<\/li>\n<li>13 :: [9] = [13, 9]<\/li>\n<li>2 :: [13, 9] = [2, 13, 9]<\/li>\n<\/ul>\n<p>Let&#8217;s create a recursive data structure<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007020;\">type<\/span> MyType a <span style=\"color: #333333;\">=<\/span>\r\n      Empty\r\n    <span style=\"color: #333333;\">|<\/span> Node a (MyType a)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The above definition means that a <em><strong>MyType<\/strong><\/em> can either be Empty or Node followed by another <strong>(MyType a)<\/strong>. A list with no element is represented as <em><strong>Empty<\/strong><\/em> while a <strong>MyType<\/strong> with one element is represented as <strong>Node a Empty.<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to create custom types in Elm. We would focus on algebraic data types and how it is implemented &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[29,30,32,31],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-elm-programming-tutorial","tag-adt","tag-algebraic-data-type","tag-recursive-type","tag-union-type"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Elm - Custom Types (Algebraic Data Types) - Elm Programming<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Elm - Custom Types (Algebraic Data Types) - Elm Programming\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to create custom types in Elm. We would focus on algebraic data types and how it is implemented &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/\" \/>\n<meta property=\"og:site_name\" content=\"Elm Programming\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-29T21:03:13+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\"},\"headline\":\"Elm &#8211; Custom Types (Algebraic Data Types)\",\"datePublished\":\"2022-05-29T21:03:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/\"},\"wordCount\":570,\"commentCount\":0,\"keywords\":[\"ADT\",\"Algebraic Data Type\",\"Recursive Type\",\"Union Type\"],\"articleSection\":[\"Elm Programming Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/\",\"name\":\"Elm - Custom Types (Algebraic Data Types) - Elm Programming\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#website\"},\"datePublished\":\"2022-05-29T21:03:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-custom-types-algebraic-data-types\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Elm &#8211; Custom Types (Algebraic Data Types)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/\",\"name\":\"Elm Programming\",\"description\":\"Elm Programming Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"sameAs\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\"],\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Elm - Custom Types (Algebraic Data Types) - Elm Programming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/","og_locale":"en_US","og_type":"article","og_title":"Elm - Custom Types (Algebraic Data Types) - Elm Programming","og_description":"In this tutorial, you will learn how to create custom types in Elm. We would focus on algebraic data types and how it is implemented &hellip;","og_url":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/","og_site_name":"Elm Programming","article_published_time":"2022-05-29T21:03:13+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e"},"headline":"Elm &#8211; Custom Types (Algebraic Data Types)","datePublished":"2022-05-29T21:03:13+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/"},"wordCount":570,"commentCount":0,"keywords":["ADT","Algebraic Data Type","Recursive Type","Union Type"],"articleSection":["Elm Programming Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/","url":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/","name":"Elm - Custom Types (Algebraic Data Types) - Elm Programming","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/elm\/#website"},"datePublished":"2022-05-29T21:03:13+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-custom-types-algebraic-data-types\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/elm\/"},{"@type":"ListItem","position":2,"name":"Elm &#8211; Custom Types (Algebraic Data Types)"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/elm\/#website","url":"https:\/\/kindsonthegenius.com\/elm\/","name":"Elm Programming","description":"Elm Programming Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/elm\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"sameAs":["https:\/\/kindsonthegenius.com\/elm"],"url":"https:\/\/kindsonthegenius.com\/elm\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/208\/revisions\/211"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}