<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vilson Fabricio Juliatto</title>
    <description>The latest articles on Forem by Vilson Fabricio Juliatto (@vfabricio).</description>
    <link>https://forem.com/vfabricio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F222787%2Fadb3acef-3163-4ab9-a00e-ff4dce7b2a69.jpg</url>
      <title>Forem: Vilson Fabricio Juliatto</title>
      <link>https://forem.com/vfabricio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vfabricio"/>
    <language>en</language>
    <item>
      <title>Understanding the Halting Problem with JavaScript</title>
      <dc:creator>Vilson Fabricio Juliatto</dc:creator>
      <pubDate>Thu, 30 Jan 2020 01:35:56 +0000</pubDate>
      <link>https://forem.com/vfabricio/understanding-the-halting-problem-with-javascript-1nlk</link>
      <guid>https://forem.com/vfabricio/understanding-the-halting-problem-with-javascript-1nlk</guid>
      <description>&lt;h1&gt;
  
  
  Understanding the Halting Problem with JavaScript
&lt;/h1&gt;

&lt;p&gt;Sometimes our code has bugs. Well, most of the time. Since our squishy, carbon-based brains are very limited to assess the correctness of our code, it would be nice to get computers to help us. That is what type checkers, linters and other kinds of static analyzers do.&lt;/p&gt;

&lt;p&gt;Here I want to focus on one particular problem our programs can have: infinite loops. This might not be the most serious type of bug in the wild. However, if we try to recruit computers to help us with it, we run into a very interesting problem. Understanding this will lead us down a fascinating rabbit hole.&lt;/p&gt;

&lt;p&gt;There are programs that always terminate (or halt, thus the name, Halting Problem) and programs that might loop forever, at least for some inputs. We would like to have a program that accepts other programs as input and tells us whether they always terminate. If you have never thought about this, stop for a minute and think about how you would try to implement such a test.&lt;/p&gt;

&lt;p&gt;Back yet? Could you do it? It turns out it is &lt;em&gt;impossible&lt;/em&gt; to do it. I am not saying it is intractable with our current hardware, or that we haven't figured out how to do it yet. It is logically, mathematically impossible to do it and I will prove that. And, since we seem to live in a world where &lt;em&gt;everything&lt;/em&gt; can be done in JavaScript, I will use that to explain the problem.&lt;/p&gt;

&lt;p&gt;This is going to be a proof by contradiction. We will start by assuming that we &lt;em&gt;can&lt;/em&gt; have a program that tells us whether any given program terminates. That will lead us to a contradiction, implying that our initial assumption is false.&lt;/p&gt;

&lt;p&gt;More concretely, imagine we had a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;halts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// some magic happens here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This should return true if &lt;code&gt;f&lt;/code&gt; halts for all inputs and return false if there are any inputs for which &lt;code&gt;f&lt;/code&gt; loops forever. For example, consider the following two functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;someRandomFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;anotherRandomFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;someRandomFunction&lt;/code&gt; always halts, but &lt;code&gt;anotherRandomFunction&lt;/code&gt; will loop forever if the first parameter is not larger than the second. Therefore, &lt;code&gt;halts(someRandomFunction)&lt;/code&gt; should be true, while &lt;code&gt;halts(anotherRandomFunction)&lt;/code&gt; should be false.&lt;/p&gt;

&lt;p&gt;Nothing weird so far, except that I have asked you to accept that the body of that &lt;code&gt;halts&lt;/code&gt; function could be filled in some meaningful way. But, if we had &lt;code&gt;halts&lt;/code&gt; at our disposal, we could write a function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;screwy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;halts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;screwy&lt;/code&gt; is a higher-order function. It, like &lt;code&gt;halts&lt;/code&gt;, accepts a function as input. Then it pulls a switcheroo on us: if the input function halts, &lt;code&gt;screwy&lt;/code&gt; loops forever; but if the input function loops forever, &lt;code&gt;screwy&lt;/code&gt; terminates. That may be a little mind bending, but it isn't absurd yet.&lt;/p&gt;

&lt;p&gt;The absurd, however, is here...&lt;/p&gt;

&lt;p&gt;Ready?&lt;/p&gt;

&lt;p&gt;What does &lt;code&gt;halts(screwy)&lt;/code&gt; return?&lt;/p&gt;

&lt;p&gt;In other words, we would like to know whether &lt;code&gt;screwy&lt;/code&gt; terminates for all inputs. If we can find any for which it doesn't, we can answer that in the negative. So, does it terminate when given &lt;em&gt;itself&lt;/em&gt; as input? That is, does &lt;code&gt;screwy(screwy)&lt;/code&gt; ever terminate?&lt;/p&gt;

&lt;p&gt;First, let's make sure this makes sense. Looking back at &lt;code&gt;screwy&lt;/code&gt;'s definition, we see that the only condition on its input is that it's a function — this comes from the same condition being imposed on the inputs to &lt;code&gt;halts&lt;/code&gt;. The input can be &lt;em&gt;any&lt;/em&gt; function. &lt;code&gt;screwy&lt;/code&gt; is a function. There is no reason, then, why it couldn't be given itself as input.&lt;/p&gt;

&lt;p&gt;But what happens then? If its input halts, &lt;code&gt;screwy&lt;/code&gt; doesn't. Therefore, if &lt;code&gt;screwy&lt;/code&gt; acts on itself and it halts, then it doesn't. Similary, if it doesn't halt, then it does.&lt;/p&gt;

&lt;p&gt;Say what??? 😲&lt;/p&gt;

&lt;p&gt;So, the existence of &lt;code&gt;screwy&lt;/code&gt; is absurd. It is logically impossible to have such a function, since that leads to a contradiction. But how can it be impossible? I showed you the definition, it is perfectly valid JavaScript... except for that sneaky call to &lt;code&gt;halts&lt;/code&gt;, which I haven't defined and whose existence we just assumed. That is the source of our paradox. If &lt;code&gt;halts&lt;/code&gt; existed we would have a contradiction. Therefore, it doesn't exist. It is impossible to have a program that always tells if another given program halts. This is the very famous Halting Problem.&lt;/p&gt;

&lt;p&gt;Let me clear up a possible misconception. I'm not saying that if you have a program in front of you it's impossible to say it whether it halts. We have seen examples both of programs that halt and programs that don't. We had no problem figuring out what was the case for each one. What the Halting Problem really says is that you can't have an algorithm that systematically answers this question for every possible program.&lt;/p&gt;

&lt;p&gt;Now you may be asking: so what? We can't have a static analyzers that always detects infinite loops. What's the big deal? The big deal is that this reveals a deep and surprising truth about the nature of computation. There are problems that can't ever be solved algorithmically. Not now, not tomorrow, not if we spend the next billion years trying to implement them. We say that they are &lt;em&gt;undecidable&lt;/em&gt;. The halting problem is the most famous, but not the only example of an undecidable problem. In fact, the standard way of proving other problems undecidable is by showing them to be equivalent to the Halting Problem. The ever useful &lt;a href="https://en.wikipedia.org/wiki/List_of_undecidable_problems"&gt;Wikipedia&lt;/a&gt; has a list such problems.&lt;/p&gt;

&lt;p&gt;That's what I had for you today folks. I hope you found it enlightening (or maybe even entertaining?)!&lt;/p&gt;

&lt;p&gt;PEDANTIC DISCLAIMER - One could argue that this is not &lt;em&gt;really&lt;/em&gt; a fully rigorous, mathematical proof, since that would require us to first define precisely the semantics of JavaScript. I don't know if this has been done rigorously so OK, fair enough. But the essential idea of the proof is what I showed and what remains is "just" to formalize it, with Turing Machines or something else.&lt;/p&gt;

</description>
      <category>halting</category>
      <category>theoretical</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Advent of Code 2019 in PureScript - Day 1, part 1</title>
      <dc:creator>Vilson Fabricio Juliatto</dc:creator>
      <pubDate>Wed, 04 Dec 2019 01:12:26 +0000</pubDate>
      <link>https://forem.com/vfabricio/advent-of-code-2019-in-purescript-day-1-part-1-3pjj</link>
      <guid>https://forem.com/vfabricio/advent-of-code-2019-in-purescript-day-1-part-1-3pjj</guid>
      <description>&lt;h1&gt;
  
  
  What's this about?
&lt;/h1&gt;

&lt;p&gt;Hi, folks! The Advent of Code 2020 is underway! That is an annual series of small programming puzzles, released daily during Advent. You can find more details about it &lt;a href="https://adventofcode.com/2019/about"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since I'm now learning PureScript, I decided to use it to solve the AoC puzzles. I intend to share my solutions here as I write them. Even though my goal is not to teach PureScript now, I hope the code will be more or less understandable even to people who are not familiar with it. If you don't understand anything, don't shy away from asking! Any suggestions of improvements will be highly appreciated too.&lt;/p&gt;

&lt;h1&gt;
  
  
  The puzzle
&lt;/h1&gt;

&lt;p&gt;The first part of the &lt;a href="https://adventofcode.com/2019/day/1"&gt;day 1&lt;/a&gt; puzzle is quite straightforward. There is a rocket, which is composed of several modules. Each module has a certain fuel requirement, according to its mass. The precise statement is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the examples and in the input given the masses are always integers, so I assumed that to always be the case. Changing the code to work with floats would be easy though, so that's not a big deal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Computing the fuel for a single module
&lt;/h2&gt;

&lt;p&gt;The implementation is quite simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;fuelFromMass&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;fuelFromMass&lt;/span&gt; &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For those not familiar with PureScript, however, there are three things that can cause confusion here.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, the &lt;code&gt;fuelFromMass :: Int -&amp;gt; Int&lt;/code&gt; line. That is a type annotation, which means that &lt;code&gt;fuelFromMass&lt;/code&gt; is a function that takes as input an &lt;code&gt;Int&lt;/code&gt; (a 32 bit integer) and returns another &lt;code&gt;Int&lt;/code&gt;. PureScript has type inference, what means that, for simple enough code, the compiler can figure out the types for itself, so that we don't always &lt;em&gt;have&lt;/em&gt; to write types explicitly. However, it is a good practice to provide annotations for top level bindings.&lt;/li&gt;
&lt;li&gt;Notice that PureScript functions don't require parentheses for their parameters. So, &lt;code&gt;mass&lt;/code&gt; is the parameter being passed to &lt;code&gt;fuelFromMass&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;What is that weird &lt;code&gt;div&lt;/code&gt; in backquotes? &lt;code&gt;div&lt;/code&gt; is just the integer division operator. The code above could also be written as
&lt;code&gt;haskell
fuelFromMass mass = (div mass 3) - 2
&lt;/code&gt;
However, Purescript allows functions that take two or more parameters to be used in infix position if they are enclosed in backquotes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Computing the total fuel
&lt;/h2&gt;

&lt;p&gt;Suppose now that we have an array containing the masses of all the modules. We need to do two things: first, compute that the fuel for each one and then sum all of the results to get the total. For someone coming from an imperative background, this sounds like it call for a loop. In functional programming we reach for a &lt;a href="https://en.wikipedia.org/wiki/Map_(higher-order_function)"&gt;map&lt;/a&gt; instead. The &lt;code&gt;map&lt;/code&gt; function takes two arguments, a function and an array and returns a new array consisting of applying the function to all elements of the input array &lt;sup id="fnref1"&gt;1&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;So, if we have an array &lt;code&gt;moduleMasses&lt;/code&gt; the corresponding fuel array is simply &lt;code&gt;map fuelFromMass moduleMasses&lt;/code&gt;. Then we use the &lt;code&gt;sum&lt;/code&gt; function to get the total value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;totalFuel&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;totalFuel&lt;/span&gt; &lt;span class="n"&gt;moduleMasses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;fuelFromMass&lt;/span&gt; &lt;span class="n"&gt;moduleMass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Actually, I wrote this as &lt;code&gt;totalFuel = sum &amp;lt;&amp;lt;&amp;lt; (map fuelFromMass)&lt;/code&gt;. I will not explain here why this is equivalent to what I showed before, because that would require me to explain currying and point-free syntax. But, if you're curious about this, ask me in the comments and I'll be glad to give more details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Input and Output
&lt;/h2&gt;

&lt;p&gt;This is all the business logic we need. Now it's just plumbing to get the input to the &lt;code&gt;totalFuel&lt;/code&gt; function and then print the result to the console. I have the input saved as a text file, where each module's mass is contained in a line. We have to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the file into memory, generating a big string.&lt;/li&gt;
&lt;li&gt;Break this string on newlines, generating an array of strings.&lt;/li&gt;
&lt;li&gt;Parse each string into a number, generating an array of &lt;code&gt;Int&lt;/code&gt;s.&lt;/li&gt;
&lt;li&gt;Pass this array to &lt;code&gt;totalFuel&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Print the return value to the console. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's tackle each point in order. First, how do we read a file? One important thing which I haven't mentioned so far is that, by default, PureScript compiles down to JavaScript. Therefore, is natural to use the functionality provided by NodeJS (more precisely, its &lt;code&gt;fs&lt;/code&gt; module) to interact with the filesystem. One can import JavaScript into PureScript but, fortunately, that will not be necessary here. There is already the &lt;a href="https://pursuit.purescript.org/packages/purescript-node-fs/5.0.1"&gt;purescript-node-fs&lt;/a&gt; library which wraps the &lt;code&gt;fs&lt;/code&gt; API into a nice, type safe set of PureScript functions. All we are going to need is the &lt;code&gt;readTextFile&lt;/code&gt; function. There are some subtleties here regarding how to deal with impure stuff in a pure functional language, involving the dreaded &lt;a href="https://wiki.haskell.org/Monad"&gt;m word&lt;/a&gt;, but that is a discussion for another day.&lt;/p&gt;

&lt;p&gt;Once we read our text file into a string, we need to split it on newlines. The &lt;a href="https://pursuit.purescript.org/packages/purescript-strings/4.0.1"&gt;purescript-strings&lt;/a&gt; library has the &lt;code&gt;split&lt;/code&gt; function which helps us write things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;splitOnNewlines&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;splitOnNewlines&lt;/span&gt; &lt;span class="n"&gt;someString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;split&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Pattern&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;someString&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The next step is a bit subtle. We need to turn a &lt;code&gt;String&lt;/code&gt; into an &lt;code&gt;Int&lt;/code&gt;. There is a function that does it for us: &lt;code&gt;fromString&lt;/code&gt; (it can be found on the &lt;code&gt;Data.Int&lt;/code&gt; module). However, its type signature, &lt;code&gt;fromString :: String -&amp;gt; Maybe Int&lt;/code&gt;, tells us that something more is going on. Why is it not simply &lt;code&gt;String -&amp;gt; Int&lt;/code&gt;? The problem is that not every string can be parsed into an integer. For strings like &lt;code&gt;"3"&lt;/code&gt;, &lt;code&gt;"58649"&lt;/code&gt; or &lt;code&gt;"-9503"&lt;/code&gt; there is no problem. But something like &lt;code&gt;"34.53"&lt;/code&gt; cannot result in an integer. And something like &lt;code&gt;"aRandomString"&lt;/code&gt; cannot be parsed into a number at all. Therefore, &lt;code&gt;fromString&lt;/code&gt; has to be able to handle failure somehow and the most basic way to do that in strongly typed functional languages is with &lt;code&gt;Maybe&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A type like &lt;code&gt;Maybe Int&lt;/code&gt; contains a special value, &lt;code&gt;Nothing&lt;/code&gt;, which denotes the &lt;em&gt;absence of a valid value&lt;/em&gt;. Then, for example, &lt;code&gt;fromString "34.53"&lt;/code&gt; returns &lt;code&gt;Nothing&lt;/code&gt;, which signals that it couldn't parse its input. But what does &lt;code&gt;fromString "3"&lt;/code&gt; return? It cannot be &lt;code&gt;3&lt;/code&gt; because that is an &lt;code&gt;Int&lt;/code&gt; and we need to return a &lt;code&gt;Maybe Int&lt;/code&gt;. The way to wrap an &lt;code&gt;Int&lt;/code&gt; into a &lt;code&gt;Maybe Int&lt;/code&gt; is with a &lt;code&gt;Just&lt;/code&gt; constructor. In other words, &lt;code&gt;fromString 3&lt;/code&gt; returns &lt;code&gt;Just 3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the end of the day, we need to work with &lt;code&gt;Int&lt;/code&gt; and not with &lt;code&gt;Maybe Int&lt;/code&gt;. So, we need to unwrap the &lt;code&gt;Just&lt;/code&gt; values and decide what to do with &lt;code&gt;Nothing&lt;/code&gt;. For the solution of this puzzle the last point doesn't really make a difference, since the given input contains only valid integers and no &lt;code&gt;Nothing&lt;/code&gt; ins generated. However, PureScript doesn't know that and it forces us to be consistent and handle failure appropriately. We do that with the &lt;code&gt;maybe&lt;/code&gt; function, which allows us to unwrap a &lt;code&gt;Maybe&lt;/code&gt; &lt;sup id="fnref2"&gt;2&lt;/sup&gt;, while providing a default value&lt;/p&gt;

&lt;p&gt;I wrote this step and the last as a single function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;readNumbers&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;readNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maybe&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;fromString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Pattern&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Don't worry if you don't fully understand this. It uses some notation and ideas that I haven't explained, but nothing that will affect what comes later.&lt;/p&gt;

&lt;p&gt;We can finally put everything together and write the complete solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Main&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Data.Int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fromString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Data.String.Common&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Data.String.Pattern&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Pattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Effect&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Effect&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Effect.Console&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Node.Encoding&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Node.FS.Sync&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;readTextFile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Prelude&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;div&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Effect&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;readTextFile&lt;/span&gt; &lt;span class="kt"&gt;ASCII&lt;/span&gt; &lt;span class="s"&gt;"./input.txt"&lt;/span&gt;
  &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"Part 1: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;totalFuel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;readNumbers&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;readNumbers&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;readNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maybe&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;fromString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Pattern&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;fuelFromMass&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;fuelFromMass&lt;/span&gt; &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mass&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="n"&gt;totalFuel&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;totalFuel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;fuelFromMass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Hopefully, even folks who are not used to PureScript can at least get the gist of this. Once again, I will be happy to (try to) answer any questions you may have. In the next post I will tackle the part 2 of this puzzle. Thanks for reading!&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;In reality, &lt;code&gt;map&lt;/code&gt; is more general than this. The second argument doesn't have to be just an array, but can be any of a class of things called &lt;em&gt;functors&lt;/em&gt;. That's why I avoided writing &lt;code&gt;map&lt;/code&gt;'s type signature, which, in it's full glory, is &lt;code&gt;map :: forall a b f. Functor f =&amp;gt; (a -&amp;gt; b) -&amp;gt; f a -&amp;gt; f b&lt;/code&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;You may find it confusing that there are two things called &lt;code&gt;maybe&lt;/code&gt;/&lt;code&gt;Maybe&lt;/code&gt;, one being a function and another being a type, differing only by capitalization. That is fine because, basically, PureScript, has two namespaces, one for names at the value-level and another for names at the type-level and it distinguishes (this is forced by the compiler) between them with capitalization. In other words, you can't write a type with a lowercase name, nor can you write a function with an capital initial. So, it's easy to know which is which when see pairs like &lt;code&gt;maybe&lt;/code&gt;/&lt;code&gt;Maybe&lt;/code&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>purescript</category>
      <category>functional</category>
      <category>adventofcode</category>
    </item>
  </channel>
</rss>
