<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Recursion on OOFP 2025-26</title>
    <link>https://unibz-oofp-25-26.github.io/docs/recursion/</link>
    <description>Recent content in Recursion on OOFP 2025-26</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <atom:link href="https://unibz-oofp-25-26.github.io/docs/recursion/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Linear recursion</title>
      <link>https://unibz-oofp-25-26.github.io/docs/recursion/sections/linear/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://unibz-oofp-25-26.github.io/docs/recursion/sections/linear/</guid>
      <description>&lt;h1 id=&#34;linear-recursion&#34;&gt;&#xA;  Linear recursion&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#linear-recursion&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h1&gt;&#xA;&lt;blockquote class=&#34;book-hint warning&#34;&gt;&#xA;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Definition.&lt;/em&gt;&lt;/strong&gt;&#xA;A recursive function/method is &lt;strong&gt;linear recursive&lt;/strong&gt; if it performs at most &lt;em&gt;one&lt;/em&gt; recursive call each time it is executed.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;blockquote class=&#34;book-hint info&#34;&gt;&#xA;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hint.&lt;/em&gt;&lt;/strong&gt;&#xA;In an imperative language (like Java),&#xA;a linear recursive algorithm can easily be transformed into an iterative one (i.e. an algorithm that uses only loops).&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Haskell does not admit loops, so linear recursion is the standard way to perform simple iterations (although &lt;a href=&#34;https://unibz-oofp-25-26.github.io/docs/control/sections/comprehension/&#34;&gt;list comprehensions&lt;/a&gt; can be used instead in certain cases).&#xA;Accordingly, in this section, we will mostly use Haskell as an illustration.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Intermediate result</title>
      <link>https://unibz-oofp-25-26.github.io/docs/recursion/sections/intermediate/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://unibz-oofp-25-26.github.io/docs/recursion/sections/intermediate/</guid>
      <description>&lt;h1 id=&#34;recursion-and-intermediate-result&#34;&gt;&#xA;  Recursion and intermediate result&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#recursion-and-intermediate-result&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h1&gt;&#xA;&lt;p&gt;In some cases, we need recursive calls to return &lt;em&gt;more information&lt;/em&gt; than what the function should compute.&lt;/p&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;We want to implement a function&lt;/p&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:#a6e22e&#34;&gt;longestWord&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;String&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Int&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;that maps a string to the length of its longest word.&lt;/p&gt;&#xA;&lt;p&gt;For simplicity, we will assume that words are separated by a blank space (noted &lt;code&gt;&#39; &#39;&lt;/code&gt; in Haskell).&lt;br&gt;&#xA;For instance,&lt;/p&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:#a6e22e&#34;&gt;longestWord&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hi there&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;should evaluate to &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://unibz-oofp-25-26.github.io/docs/haskell/sections/list/#string&#34;&gt;Recall&lt;/a&gt; that a string in Haskell is an array of characters.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Accumulator and tail recursion</title>
      <link>https://unibz-oofp-25-26.github.io/docs/recursion/sections/accumulator/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://unibz-oofp-25-26.github.io/docs/recursion/sections/accumulator/</guid>
      <description>&lt;h1 id=&#34;accumulator-and-tail-recursion&#34;&gt;&#xA;  Accumulator and tail recursion&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#accumulator-and-tail-recursion&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h1&gt;&#xA;&lt;h2 id=&#34;tail-recursion&#34;&gt;&#xA;  Tail recursion&#xA;  &lt;a class=&#34;anchor&#34; href=&#34;#tail-recursion&#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;Definition.&lt;/em&gt;&lt;/strong&gt;&#xA;A function/method is &lt;strong&gt;tail-recursive&lt;/strong&gt; if it is:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://unibz-oofp-25-26.github.io/docs/recursion/sections/linear/&#34;&gt;linear recursive&lt;/a&gt;, and&lt;/li&gt;&#xA;&lt;li&gt;does not perform any operation after a recursive call.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&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;The following function is tail-recursive&lt;/p&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;-- `True` iff the input string ends with an `s`&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:#a6e22e&#34;&gt;endsWithS&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;::&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;String&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Bool&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:#a6e22e&#34;&gt;endsWithS&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;[]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;False&lt;/span&gt;               &lt;span style=&#34;color:#75715e&#34;&gt;-- first base case&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:#a6e22e&#34;&gt;endsWithS&lt;/span&gt; [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;s&amp;#39;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;             &lt;span style=&#34;color:#75715e&#34;&gt;-- second base case&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:#a6e22e&#34;&gt;endsWithS&lt;/span&gt; (&lt;span style=&#34;color:#66d9ef&#34;&gt;_&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;:&lt;/span&gt; cs) &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; endsWithS cs  &lt;span style=&#34;color:#75715e&#34;&gt;-- inductive case&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;The function &lt;code&gt;sum&lt;/code&gt; seen &lt;a href=&#34;https://unibz-oofp-25-26.github.io/docs/recursion/sections/linear/&#34;&gt;earlier&lt;/a&gt; (and reproduced here) is &lt;em&gt;not&lt;/em&gt; tail-recursive, because the addition (Line 3) is performed &lt;em&gt;after&lt;/em&gt; termination of the recursive call.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
