<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Composite type on OOFP 2025-26</title>
    <link>https://unibz-oofp-25-26.github.io/docs/composite/</link>
    <description>Recent content in Composite type on OOFP 2025-26</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <atom:link href="https://unibz-oofp-25-26.github.io/docs/composite/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Enumerated type</title>
      <link>https://unibz-oofp-25-26.github.io/docs/composite/sections/enum/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://unibz-oofp-25-26.github.io/docs/composite/sections/enum/</guid>
      <description>&lt;h1 id=&#34;enumerated-type&#34;&gt;&#xA;  Enumerated type&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#enumerated-type&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h1&gt;&#xA;&lt;p&gt;An enumerated type is a finite set of alternative values.&lt;/p&gt;&#xA;&lt;!-- This is a more readable alternative to so-called &#34;magic numbers&#34;, --&gt;&#xA;&lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example.&lt;/em&gt;&lt;/strong&gt;&#xA;A playing card can have one of four suits: Clubs, Diamonds, Hearts or Spades.&lt;/p&gt;&#xA;&lt;p&gt;In the &lt;a href=&#34;https://unibz-oofp-25-26.github.io/docs/ass/sections/ass1/&#34;&gt;first assignment&lt;/a&gt;, we modeled these values as four characters &lt;code&gt;&#39;C&#39;&lt;/code&gt;, &lt;code&gt;&#39;D&#39;&lt;/code&gt;, &lt;code&gt;&#39;H&#39;&lt;/code&gt; or &lt;code&gt;&#39;S&#39;&lt;/code&gt;.&#xA;This solution is unsatisfying, because the type &lt;code&gt;Char&lt;/code&gt; allows other letters.&#xA;Instead, we could declare an enumerated type (for instance named &lt;code&gt;Suit&lt;/code&gt;) with 4 possible values (&lt;code&gt;Clubs&lt;/code&gt;, &lt;code&gt;Diamonds&lt;/code&gt;, &lt;code&gt;Hearts&lt;/code&gt; and &lt;code&gt;Spades&lt;/code&gt;).&lt;/p&gt;</description>
    </item>
    <item>
      <title>Product type</title>
      <link>https://unibz-oofp-25-26.github.io/docs/composite/sections/product/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://unibz-oofp-25-26.github.io/docs/composite/sections/product/</guid>
      <description>&lt;h1 id=&#34;product-type&#34;&gt;&#xA;  Product type&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#product-type&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h1&gt;&#xA;&lt;!-- A product type describes values that consist of valuessimplistic --&gt;&#xA;&lt;p&gt;A product type can be viewed as a simplistic &lt;a href=&#34;https://unibz-oofp-25-26.github.io/notreleased/&#34;&gt;class&lt;/a&gt;, and its values as simplistic &lt;a href=&#34;https://unibz-oofp-25-26.github.io/notreleased/&#34;&gt;objects&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;in-haskell&#34;&gt;&#xA;  in Haskell&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#in-haskell&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&lt;!-- &lt;blockquote class=&#34;book-hint warning&#34;&gt;&#xA;   --&gt;&#xA;&lt;!-- **_Syntax._** --&gt;&#xA;&lt;!-- Like an [enumerated type](), a product type in Haskell can be declared with the keyword `data`, as follows --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- ``` --&gt;&#xA;&lt;!-- data &lt;product type name&gt; = &lt;definition&gt; --&gt;&#xA;&lt;!-- ``` --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- &#xA;&lt;/blockquote&gt;&#xA; --&gt;&#xA;&lt;!-- There are two alternative syntaxes for this `&lt;definition&gt;`: --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- - a [tuple type](), or --&gt;&#xA;&lt;!-- - a definition with a [constructor](). --&gt;&#xA;&lt;!-- ### Definition as a tuple type --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- &lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;   --&gt;&#xA;&lt;!-- **_Example._** --&gt;&#xA;&lt;!-- The declaration below defines a product type `Interval`. --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- ```haskell --&gt;&#xA;&lt;!-- -- a temporal interval, represented as two timestamps (start and end) --&gt;&#xA;&lt;!-- data Interval = (Int, Int) --&gt;&#xA;&lt;!-- ``` --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- In this definition, `(Int, Int)` is the type of pairs of integers. --&gt;&#xA;&lt;!-- &#xA;&lt;/blockquote&gt;&#xA; --&gt;&#xA;&lt;!-- &lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;   --&gt;&#xA;&lt;!-- **_Example._** --&gt;&#xA;&lt;!-- The expression below define a product type `Signal`. --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- A `Signal` consists of: --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- - an `Interval`, and --&gt;&#xA;&lt;!-- - a value for the enumerated type `Light` seen [earlier](). --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- ```haskell --&gt;&#xA;&lt;!-- -- a traffic signalling event, represented as a temporal interval and the light (Green, Yellow or Red) during that interval --&gt;&#xA;&lt;!-- data Signal = (Interval, Light) --&gt;&#xA;&lt;!-- ``` --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- In this definition, `(Interval, Light)` is the type of all pairs with: --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- - a first element of type `Interval`, and --&gt;&#xA;&lt;!-- - a second element of type `Light`. --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- &#xA;&lt;/blockquote&gt;&#xA; --&gt;&#xA;&lt;!-- ### Instances --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- &lt;blockquote class=&#34;book-hint warning&#34;&gt;&#xA;   --&gt;&#xA;&lt;!-- **_Syntax (reminder)._** --&gt;&#xA;&lt;!-- A tuple in Haskell is written in parentheses. --&gt;&#xA;&lt;!-- &#xA;&lt;/blockquote&gt;&#xA; --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- &lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;   --&gt;&#xA;&lt;!-- **_Examples._** --&gt;&#xA;&lt;!-- Consider the two product types that we just defined: --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- ```haskell --&gt;&#xA;&lt;!-- data Interval = (Int, Int) --&gt;&#xA;&lt;!-- data Signal = (Interval, Light) --&gt;&#xA;&lt;!-- ``` --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- Then: --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- - the pair `(5,8)` has type `Interval`, --&gt;&#xA;&lt;!-- - the pair `((5,8), Red)` has type `Signal`. --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- &#xA;&lt;/blockquote&gt;&#xA; --&gt;&#xA;&lt;!-- ### Definition with a constructor --&gt;&#xA;&lt;!-- Haskell provides another syntax to define a product type, by means of a **_constructor_**. --&gt;&#xA;&lt;!-- As we will see later, when combined with [sum types](), this syntax allows: --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- - concise pattern matching, and --&gt;&#xA;&lt;!-- - [recursive types](). --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- So this is often the _preferred way_ to define a product type. --&gt;&#xA;&lt;h3 id=&#34;declaration&#34;&gt;&#xA;  Declaration&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#declaration&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h3&gt;&#xA;&lt;blockquote class=&#34;book-hint warning&#34;&gt;&#xA;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Syntax.&lt;/em&gt;&lt;/strong&gt;&#xA;A product type can be declared as&lt;/p&gt;</description>
    </item>
    <item>
      <title>Sum type</title>
      <link>https://unibz-oofp-25-26.github.io/docs/composite/sections/sum/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://unibz-oofp-25-26.github.io/docs/composite/sections/sum/</guid>
      <description>&lt;h1 id=&#34;sum-type&#34;&gt;&#xA;  Sum type&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#sum-type&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h1&gt;&#xA;&lt;p&gt;A sum type represents several &lt;em&gt;alternative&lt;/em&gt; types.&lt;/p&gt;&#xA;&lt;h2 id=&#34;in-haskell&#34;&gt;&#xA;  in Haskell&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#in-haskell&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&lt;blockquote class=&#34;book-hint warning&#34;&gt;&#xA;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Syntax.&lt;/em&gt;&lt;/strong&gt;&#xA;A sum type in Haskell is declared as&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;data &amp;lt;sum type name&amp;gt; = &amp;lt;type 1&amp;gt; | ... | &amp;lt;type n&amp;gt;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;&#xA;&lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example.&lt;/em&gt;&lt;/strong&gt;&#xA;In the following declarations:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;Point&lt;/code&gt; is a &lt;a href=&#34;https://unibz-oofp-25-26.github.io/docs/composite/sections/product/&#34;&gt;product type&lt;/a&gt;, and&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;Shape&lt;/code&gt; is a sum type.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-haskell&#34; data-lang=&#34;haskell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;-- a point in the Euclidean plane, represented by its coordinates&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;data&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;PointC&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Float&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Float&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;-- a shape, defined as either a triangle or a rectangle&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;data&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Shape&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;TriangleC&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;RectangleC&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Point&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;&#xA;&lt;!-- &lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;   --&gt;&#xA;&lt;!-- **_Observation._** In this example, we use constructors (`TriangleC` and `RectangleC`) directly in the definition of the type `Shape`. --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- Alternatively, we could have introduced names for the triangle and rectangle types, as follows: --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- ```haskell --&gt;&#xA;&lt;!-- data Shape = Triangle | Rectangle --&gt;&#xA;&lt;!-- data Triangle = TriangleC Point Point Point --&gt;&#xA;&lt;!-- data Rectangle = RectangleC Point Point Point Point --&gt;&#xA;&lt;!-- ``` --&gt;&#xA;&lt;!----&gt;&#xA;&lt;!-- &#xA;&lt;/blockquote&gt;&#xA; --&gt;&#xA;&lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note.&lt;/em&gt;&lt;/strong&gt;&#xA;in Haskell, an &lt;a href=&#34;https://unibz-oofp-25-26.github.io/docs/composite/sections/enum/&#34;&gt;enumerated type&lt;/a&gt; is a sum type.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
