<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://www.codefugue.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://www.codefugue.com/" rel="alternate" type="text/html" /><updated>2020-10-04T18:04:54+00:00</updated><id>https://www.codefugue.com/feed.xml</id><title type="html">Code Fugue</title><subtitle>software development blog of calvin bottoms</subtitle><entry><title type="html">Music Update</title><link href="https://www.codefugue.com/music-update/" rel="alternate" type="text/html" title="Music Update" /><published>2020-09-17T00:00:00+00:00</published><updated>2020-09-17T00:00:00+00:00</updated><id>https://www.codefugue.com/music-update</id><content type="html" xml:base="https://www.codefugue.com/music-update/">&lt;p&gt;Hey, I’m trying to step away from social media, which has reminded me that I have
this blog. My musical life has picked up a bit in the last couple of years, and I
thought I would throw out an update here just for fun.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/calvin-ter-arotr-2019-800.jpg&quot; alt=&quot;Me at Abbey Road on the River 2019&quot; /&gt;
&lt;em&gt;Photo by Paul Martin&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In early 2018 I started playing keyboards in a Beatles cover band called
&lt;a href=&quot;https://theenglishrain.com&quot; target=&quot;_blank&quot;&gt;The English Rain&lt;/a&gt;.
Sorry to brag, but these guys are really good, and I work hard to keep up with them.
No, we don’t dress up in wigs and suits; we just try to recreate the original sound as
faithfully as possible. This means Epiphones, Gretsches, Rickenbackers, a Hofner bass,
and lots of attention to vocal harmonies.
We usually have a regular monthly outdoor gig in downtown
Franklin through the summer, but this year, for obvious reasons, we did not.&lt;/p&gt;

&lt;p&gt;To keep ourselves entertained during the lockdown, we made a few quarantine home
performance videos of some of our favorite 70s and 80s tunes. We used the name
“The Shut-Inz” so people wouldn’t think The English Rain had given up on The Beatles.
Yes, I am trying to avoid social media, but if you’re still playing that game
you can see these videos on our
&lt;a href=&quot;https://facebook.com/theenglishrain&quot; target=&quot;_blank&quot;&gt;Facebook page&lt;/a&gt;
and/or our
&lt;a href=&quot;https://www.youtube.com/channel/UCSOWwr1ngWQl-FH_3e4pKiw/&quot; target=&quot;_blank&quot;&gt;YouTube channel&lt;/a&gt;.
The sound is better on YouTube, but on Facebook there are also some live stream replays
if you’re into that kind of thing.&lt;/p&gt;

&lt;p&gt;Since this is offically a software blog, I’ll mention that I have done a good bit
of coding for my laptop-based keyboard rig. Sometimes I don’t have quite enough hands
to cover all the parts and have to use a little magic. This is how I have approached
“Martha My Dear” and “The Long and Winding Road,” and I’m still trying to figure out
how to make “Honey Pie” happen. Maybe I’ll write more about that later.&lt;/p&gt;

&lt;p&gt;I have also been playing keyboards with a theme-driven band called
&lt;a href=&quot;https://www.instagram.com/thedecadesbandnashville/&quot; target=&quot;_blank&quot;&gt;The Decades&lt;/a&gt;.
They are a talented bunch of twenty-somethings with an inexplicable knowledge of and
affection for the music of my generation.
This is the least efficient band possible; so far each gig has had a completely
different set list, hardly ever recycling anything.
It’s a lot of work, but it never gets boring. Thus far we have done:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;70s and 80s Classic Rock&lt;/li&gt;
  &lt;li&gt;90s Radio Pop&lt;/li&gt;
  &lt;li&gt;Christmas Tunes&lt;/li&gt;
  &lt;li&gt;Country Originals&lt;/li&gt;
  &lt;li&gt;Disco!&lt;/li&gt;
  &lt;li&gt;Movie Songs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These band endeavors have also led to the opportunity for me to do a little session work,
which I have not done in many, many years. I am very fortunate at my age to be still
learning and growing in the things that I love. It was the hope of doing these kinds of
things that drew me to Nashville in the first place. I’m still happy to be here.&lt;/p&gt;

&lt;p&gt;Whatever it is that you love to do outside of work, I hope you’re finding or making 
opportunities to do it. Have fun out there, and please stay safe!&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="music" /><summary type="html">Hey, I’m trying to step away from social media, which has reminded me that I have this blog. My musical life has picked up a bit in the last couple of years, and I thought I would throw out an update here just for fun.</summary></entry><entry><title type="html">OH: Confessions of a Tweet Thief</title><link href="https://www.codefugue.com/confessions-of-a-tweet-thief/" rel="alternate" type="text/html" title="OH: Confessions of a Tweet Thief" /><published>2017-10-15T01:33:11+00:00</published><updated>2017-10-15T01:33:11+00:00</updated><id>https://www.codefugue.com/confessions-of-a-tweet-thief</id><content type="html" xml:base="https://www.codefugue.com/confessions-of-a-tweet-thief/">&lt;p&gt;Most of my developer friends who have achieved any amount of public recognition or notoriety have reported experiencing some measure of &lt;a href=&quot;https://en.wikipedia.org/wiki/Impostor_syndrome&quot;&gt;impostor syndrome&lt;/a&gt;. They feel that they have garnered a reputation that surpasses their actual level of competence and contribution. It always brings me great comfort to hear this, because I feel the same way. Or am I just pretending so that I’ll feel more like one of them? 😉&lt;/p&gt;

&lt;p&gt;Well, I recently went beyond the syndrome and proved myself an actual impostor, a fraud, a phony. A plagiarist, to be specific. One evening last month I was sitting at home doing something useless on my laptop when my eldest spawn told me a geeky math joke. I thought it was very clever. I thought my geeky friends would appreciate it as well. So I tweeted it.&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;This Fibonacci joke is as bad as the last two you heard combined.&lt;/p&gt;&amp;mdash; Calvin Bottoms (@calvinb) &lt;a href=&quot;https://twitter.com/calvinb/status/907794337492332544?ref_src=twsrc%5Etfw&quot;&gt;September 13, 2017&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;The next morning I awoke to a deluge of notifications of responses, likes, and retweets beyond anything I’ve ever experienced on Twitter. This tweet had taken flight for some reason. I got replies from some of my geek heroes who neither know me nor follow me.&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;I have a feeling the next one will be at least as bad again.&lt;/p&gt;&amp;mdash; Dan North (@tastapod) &lt;a href=&quot;https://twitter.com/tastapod/status/907998170847830017?ref_src=twsrc%5Etfw&quot;&gt;September 13, 2017&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;I was at work when I saw the notification that I had been retweeted by Australian comedian &lt;a href=&quot;https://en.wikipedia.org/wiki/Tim_Minchin&quot;&gt;Tim Minchin&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Oh yes. &lt;a href=&quot;https://t.co/pS1YPyKcBj&quot;&gt;https://t.co/pS1YPyKcBj&lt;/a&gt;&lt;/p&gt;&amp;mdash; Tim Minchin (@timminchin) &lt;a href=&quot;https://twitter.com/timminchin/status/908043745316057088?ref_src=twsrc%5Etfw&quot;&gt;September 13, 2017&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;What was happening? I guess it was my twenty minutes of Twitter fame. I started looking through the responses. One that caught my eye (and my stomach) was addressed to &lt;a href=&quot;https://twitter.com/sigfpe&quot;&gt;@sigfpe&lt;/a&gt; and said simply,&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Congrats on getting your tweet stolen &lt;a href=&quot;https://t.co/3D2WriOhAK&quot;&gt;https://t.co/3D2WriOhAK&lt;/a&gt;&lt;/p&gt;&amp;mdash; Kevin Lin (@quasicoherence) &lt;a href=&quot;https://twitter.com/quasicoherence/status/908134907251183617?ref_src=twsrc%5Etfw&quot;&gt;September 14, 2017&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;Oh, no. I had stolen this man’s tweet. In retrospect I suppose I could have googled the joke so I could attribute it properly, but I wasn’t writing a research paper, and I had no idea anyone other than my geeky friends would even see it. I composed a quick explanation/apology tweet to &lt;a href=&quot;https://twitter.com/sigfpe&quot;&gt;@sigfpe&lt;/a&gt;, confessing that I owed him a large number of retweets. It was one those times that 140 characters did not feel like enough, but he could not have been more gracious about it, and I am very grateful.&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;If your kid told you it that makes my day 😁&lt;/p&gt;&amp;mdash; Dan Piponi (@sigfpe) &lt;a href=&quot;https://twitter.com/sigfpe/status/908179972610605056?ref_src=twsrc%5Etfw&quot;&gt;September 14, 2017&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;Such social media points mean nothing to some people but an awful lot to others, and I had just collected someone else’s jackpot. So &lt;a href=&quot;https://twitter.com/sigfpe&quot;&gt;@sigfpe&lt;/a&gt;, thank you for being so cool about this whole thing and seeing it for the inadvertent act that it was. I really liked your joke, and I clearly am not alone.&lt;/p&gt;

&lt;p&gt;Man, Twitter is kind of frightening. It’s fortunate that important people don’t use it to talk about real things.&lt;/p&gt;

&lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;</content><author><name>Calvin Bottoms</name></author><category term="social" /><summary type="html">Most of my developer friends who have achieved any amount of public recognition or notoriety have reported experiencing some measure of impostor syndrome. They feel that they have garnered a reputation that surpasses their actual level of competence and contribution. It always brings me great comfort to hear this, because I feel the same way. Or am I just pretending so that I’ll feel more like one of them? 😉</summary></entry><entry><title type="html">Nodevember 2016</title><link href="https://www.codefugue.com/nodevember-2016/" rel="alternate" type="text/html" title="Nodevember 2016" /><published>2016-11-17T04:05:00+00:00</published><updated>2016-11-17T04:05:00+00:00</updated><id>https://www.codefugue.com/nodevember-2016</id><content type="html" xml:base="https://www.codefugue.com/nodevember-2016/">&lt;p&gt;I just got done with &lt;a href=&quot;http://nodevember.org/&quot;&gt;Nodevember&lt;/a&gt; 2016, where I saw some great talks, caught up with some old friends, and made some new ones. I also had a fun time hanging out in person with some of my &lt;a href=&quot;https://www.leankit.com&quot;&gt;LeanKit&lt;/a&gt; co-workers who work remotely, and I work with the greatest people in the world, so that’s always wonderful. I had the privilege of presenting a new talk I called “Arts and Sciences: Programming Music on the Web.” I had a fabulous crowd who were so onboard and engaged, and they made it so much fun! If you were there, thank you for making it such a blast!&lt;/p&gt;

&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/BI07FgIDEYM&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;The slides for my talk are available &lt;a href=&quot;https://docs.google.com/presentation/d/1XRB9_aLJa-hV9CKE60cQt4_vfQAFw_iaGK5H9P8jWpE&quot;&gt;here&lt;/a&gt;, and you can find the “Beep Boop” BASIC code I showed at https://github.com/calvinb/beep-boop.&lt;/p&gt;

&lt;p&gt;In case you’re interested in the recordings of the early computer music demos I mentioned, here are the links:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.theguardian.com/science/2016/sep/26/first-recording-computer-generated-music-created-alan-turing-restored-enigma-code&quot;&gt;Turing and Strachey 1951&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=fgYhVnmeWrk&quot;&gt;Dompier, Altair 8800b “Fool on the Hill” 1975&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go build something fun!&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="javascript" /><category term="music" /><summary type="html">I just got done with Nodevember 2016, where I saw some great talks, caught up with some old friends, and made some new ones. I also had a fun time hanging out in person with some of my LeanKit co-workers who work remotely, and I work with the greatest people in the world, so that’s always wonderful. I had the privilege of presenting a new talk I called “Arts and Sciences: Programming Music on the Web.” I had a fabulous crowd who were so onboard and engaged, and they made it so much fun! If you were there, thank you for making it such a blast!</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pbs.twimg.com/profile_images/642086927173316608/wi7FHPE7.png" /><media:content medium="image" url="https://pbs.twimg.com/profile_images/642086927173316608/wi7FHPE7.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">F-Words Slides</title><link href="https://www.codefugue.com/f-words-slides/" rel="alternate" type="text/html" title="F-Words Slides" /><published>2014-09-26T22:02:00+00:00</published><updated>2014-09-26T22:02:00+00:00</updated><id>https://www.codefugue.com/f-words-slides</id><content type="html" xml:base="https://www.codefugue.com/f-words-slides/">&lt;p&gt;Thanks to everyone who came to my &quot;F-Words&quot; talk on functional programming terms at CoderFaire Tennessee. Here are the slides.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.google.com/presentation/d/1CP8I2rr4ZuQ1l7iJz7MTbJXTXdFoVHug7QRw7Mdzpgc/edit?usp=sharing&quot;&gt;https://docs.google.com/presentation/d/1CP8I2rr4ZuQ1l7iJz7MTbJXTXdFoVHug7QRw7Mdzpgc/edit?usp=sharing&lt;/a&gt;&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="functional" /><summary type="html">Thanks to everyone who came to my &quot;F-Words&quot; talk on functional programming terms at CoderFaire Tennessee. Here are the slides.</summary></entry><entry><title type="html">Partial Application vs. Currying</title><link href="https://www.codefugue.com/partial-application-vs-currying/" rel="alternate" type="text/html" title="Partial Application vs. Currying" /><published>2014-06-28T08:06:00+00:00</published><updated>2014-06-28T08:06:00+00:00</updated><id>https://www.codefugue.com/partial-application-vs-currying</id><content type="html" xml:base="https://www.codefugue.com/partial-application-vs-currying/">&lt;p&gt;Is the entire world of aspiring functional programmers generally confused about partial function application and currying, or is it just me? I like to feel well-informed when I think to myself that these two are not the same thing, but I still couldn’t tell you what the difference is. To me they are like Bill and Ted on their Excellent Adventure; I know them when I see them, but I don’t know which is which. I have just decided it is time to dive in and get this straight. So here we go.&lt;/p&gt;

&lt;h3 id=&quot;divide-and-conquer&quot;&gt;Divide and Conquer&lt;/h3&gt;

&lt;p&gt;Partial function application and currying both have to do with decomposing multi-parameter functions into smaller functions with fewer parameters. For example, in Haskell we might have an &lt;em&gt;add&lt;/em&gt; function that takes two integers and returns their sum:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If I had this function available and wanted another function that took one number and added 3 to it, I could do this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;add3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;add3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now this function does just what we want.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;add3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When I see this kind of thing, I can say with confidence, “Ah, that’s that currying/partial application deal.” But which is it? Either one? Both?&lt;/p&gt;

&lt;p&gt;After spending a little time digging in, I think my main problem here is that I’m only concerned about these concepts when working in languages whose functions support it implicitly, in my case Haskell and F#. I get these two bits of functional magic free in a gift bag, but since they’re always wrapped up together, they feel like one thing instead of two.&lt;/p&gt;

&lt;h3 id=&quot;break-it-down&quot;&gt;Break It Down&lt;/h3&gt;

&lt;p&gt;Of the two concepts, partial application is the easier to understand, thanks to its meaningful name. It is just what it sounds like. You have a function that takes more than one parameter, you pass it fewer parameters than it wants, and it returns a new function that will take the remaining parameters and return you your result, as we just demonstrated with our &lt;em&gt;add3&lt;/em&gt; function. It is partially applied, applied to some of its parameters with the option to apply the resulting function to the rest later.&lt;/p&gt;

&lt;p&gt;So what is currying? Currying is the transformation of one multi-parameter function into a chain of single-parameter functions that ultimately yield the same result. The problem with trying to discuss currying in Haskell and F# is that they don’t have multi-parameter functions in first place. A function such as &lt;em&gt;add&lt;/em&gt;, which logically has multiple parameters, is defined implicitly as a chain of single-parameter functions, or in the &lt;em&gt;curried&lt;/em&gt; style. So when does the currying take place? I would have to say at compile time. No wonder I was missing it!&lt;/p&gt;

&lt;p&gt;In Haskell and F#, currying is more their style of implementing multi-parameter functions than it is a technique that we use when writing in these languages. The functions are curried, period, whether or not we choose to partially apply them, which is the only time we would think to notice.&lt;/p&gt;

&lt;h3 id=&quot;build-it&quot;&gt;Build It&lt;/h3&gt;

&lt;p&gt;To get a feel for what is involved here, we need to get away from this implicit currying and go somewhere we have support for true multi-parameter functions, and we can build this stuff ourselves. JavaScript, anyone? JavaScript, like most imperative languages, and unlike Haskell and F#, supports true (non-curried) multi-parameter functions. (Its &lt;em&gt;bind&lt;/em&gt; function provides partial application support as well, but we still want to build our own so we can see what’s happening.) Let’s start with our &lt;em&gt;add&lt;/em&gt; function again.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we can build a function to partially apply another two-parameter function by taking it and its first parameter, and returning a new function that takes the second parameter and returns the result.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;papply&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fn2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we can use this function to build our &lt;em&gt;add3&lt;/em&gt; function by partially applying &lt;em&gt;add&lt;/em&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;papply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;five&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//--&amp;gt; 5&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Notice how that was more work than partially applying in Haskell? That’s because a curried function gives you partial application pretty much free; the single-parameter component functions are already in there, just waiting to be let out. Let’s build a &lt;em&gt;curry&lt;/em&gt; function in JavaScript to demonstrate this. Remember currying is the transformation of a multi-parameter function into a chain of single-parameter functions, so our &lt;em&gt;curry&lt;/em&gt; function will take a function and return a function. Let’s assume our input function takes two parameters, like our &lt;em&gt;add&lt;/em&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;curry&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can use this function to create a curried version of &lt;em&gt;add&lt;/em&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cAdd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;curry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This function will return the same results as &lt;em&gt;add&lt;/em&gt; but has to be called differently, since it is a chain of single-parameter functions.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;five&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cAdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//--&amp;gt; 5&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now back to the idea of how the curried style gives us partial application free of charge. Look at how we can partially apply our &lt;em&gt;cAdd&lt;/em&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cAdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;seven&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//--&amp;gt; 7&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Do you see what we’ve got here? Currying is a way of defining a function; partial application is a way of applying a function. Currying is about construction; partial application is about consumption, with a by-product. Some functions are curried, but no functions are partial.&lt;/p&gt;

&lt;h3 id=&quot;undo&quot;&gt;Undo&lt;/h3&gt;

&lt;p&gt;In the words of Blood, Sweat, and Tears, what goes up must come down, and curried functions can be uncurried (in languages that support multi-parameter functions). Uncurrying would be the transformation of a chain of single-parameter functions into one multi-parameter function. Here is a function that will do that for us:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uncurry&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cfn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cfn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we could use this function to create a new &lt;em&gt;add&lt;/em&gt; function from our curried &lt;em&gt;cAdd&lt;/em&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;newAdd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uncurry&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cAdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;eight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;newAdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;//--&amp;gt; 8&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Excellent! We have separated partial application from currying. I think I might be able to remember the difference now. I hope you will, too.&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="haskell" /><category term="functional" /><category term="f#" /><category term="javascript" /><summary type="html">Is the entire world of aspiring functional programmers generally confused about partial function application and currying, or is it just me? I like to feel well-informed when I think to myself that these two are not the same thing, but I still couldn’t tell you what the difference is. To me they are like Bill and Ted on their Excellent Adventure; I know them when I see them, but I don’t know which is which. I have just decided it is time to dive in and get this straight. So here we go.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://www.foodpeoplewant.com/wp-content/uploads/2009/11/Panang-Beef-Curry-1024x768.jpg" /><media:content medium="image" url="http://www.foodpeoplewant.com/wp-content/uploads/2009/11/Panang-Beef-Curry-1024x768.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Magic Not in the Monad</title><link href="https://www.codefugue.com/magic-not-in-the-monad/" rel="alternate" type="text/html" title="Magic Not in the Monad" /><published>2014-05-13T07:46:00+00:00</published><updated>2014-05-13T07:46:00+00:00</updated><id>https://www.codefugue.com/magic-not-in-the-monad</id><content type="html" xml:base="https://www.codefugue.com/magic-not-in-the-monad/">&lt;p&gt;I am recently coming to grips with a foundational misconception I have been entertaining about monads, possibly since I first heard the term. I am not a category theorist, and I first encountered monads in the context of functional programming. For me the monad has always been shrouded in mystery and sporting a reputation of being very difficult to understand.&lt;/p&gt;

&lt;p&gt;My misconception is that I have associated monads with side effects. Before I started learning Haskell, I heard someone say, “If you need to produce a side effect in Haskell, you have to use a monad.” This is not untrue, but the way it is stated is misleading. It would be more accurate to be more specific and say you have to use the IO type constructor. But IO is a monad, so where’s the harm?&lt;/p&gt;

&lt;p&gt;It didn’t help me that IO is so often referred to as “the IO monad.” IO’s imperative side effect business makes it a unique thing in the Haskell world. I attributed at least some of that uniqueness to its monad-ness, but the two have nothing to do with one another. Other monads do not have side effects, and IO can wield its side effects without being treated like a monad.&lt;/p&gt;

&lt;p&gt;The epiphany of my error occurred during a recent &lt;a href=&quot;http://nashfp.org&quot;&gt;NashFP&lt;/a&gt; discussion in which I found myself surprised to see that Maybe was a monad. The Maybe type class, being perhaps the simplest example of a context, is a great case to consider since there is minimal magic to get distracted by. How could Maybe be a monad when monads are magic and Maybe is not? I had to take a look and see what other non-magical monads were hanging around.&lt;/p&gt;

&lt;p&gt;We can use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:info&lt;/code&gt; command to ask ghci (Glasgow Haskell Compiler Interactive) what Monad instances it has loaded right out of the box.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kt&quot;&gt;Prelude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `Data.Maybe'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `Data.Either'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here we see first the four functions of the Monad type class, and then the instances, which are what we’re after. There are five Monad instances in GHC.Base, and they are Maybe, Either, list, IO, and function. Yeah, the syntax is weird, but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;((-&amp;gt;) r)&lt;/code&gt; is the &lt;em&gt;function&lt;/em&gt; function. Functions are monads. Go chew on that one awhile. Every one of these Monad instances except IO is utterly commonplace and non-magical.&lt;/p&gt;

&lt;p&gt;So there is monad without magic. How about magic without monad? What do we know about IO other than that it’s a Monad instance? Let’s ask ghci.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kt&quot;&gt;Prelude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;GHC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Types&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;GHC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Prim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;GHC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Prim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RealWorld&lt;/span&gt;
                  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;GHC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Prim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;GHC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Prim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RealWorld&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Types'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Monad&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Of course! It’s a Functor, as Monads almost always are. Let’s talk to it like a plain old Functor. How do we talk Functor? Back to ghci.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kt&quot;&gt;Prelude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;GHC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&amp;lt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `Data.Maybe'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `Data.Either'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((,)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- Defined in `GHC.Base'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Ah, yes, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; is the only function in the Functor type class. If IO is just a Functor, we should be able to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; over it. What is the simplest way to get an IO value? My favorite is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getLine&lt;/code&gt; function, which will give us an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IO String&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kt&quot;&gt;Prelude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getLine&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;getLine&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; over an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IO String&lt;/code&gt;, we need a function that takes a String and returns a simple value. Let’s use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(++&quot;!&quot;)&lt;/code&gt;, which will “shout” any string we apply it to. A simple use would look like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kt&quot;&gt;Prelude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hi&quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;Hi!&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now let’s try to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt; that function over an IO String, which we will get from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getLine&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kt&quot;&gt;Prelude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getLine&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Hello&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;Hello!&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Look at that. We just used IO as a Functor (via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmap&lt;/code&gt;) without using any Monad functions or features. It still waited for input. It still got an unpredictable value. It still did its magic thing as a plain old Functor.&lt;/p&gt;

&lt;p&gt;So there we have it. The magic is in IO alone, not in Monad. I’ll try to write more soon about what the monad mojo &lt;em&gt;is&lt;/em&gt;, now that we have spent some time on what it is &lt;em&gt;not&lt;/em&gt;.&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="haskell" /><category term="monads" /><summary type="html">I am recently coming to grips with a foundational misconception I have been entertaining about monads, possibly since I first heard the term. I am not a category theorist, and I first encountered monads in the context of functional programming. For me the monad has always been shrouded in mystery and sporting a reputation of being very difficult to understand.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://upload.wikimedia.org/wikipedia/commons/c/c2/DeeHieroglyph.gif" /><media:content medium="image" url="http://upload.wikimedia.org/wikipedia/commons/c/c2/DeeHieroglyph.gif" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Rebuilding Haskell in C#: Functors</title><link href="https://www.codefugue.com/haskell-in-c-sharp-functors/" rel="alternate" type="text/html" title="Rebuilding Haskell in C#: Functors" /><published>2014-01-20T21:23:00+00:00</published><updated>2014-01-20T21:23:00+00:00</updated><id>https://www.codefugue.com/haskell-in-c-sharp-functors</id><content type="html" xml:base="https://www.codefugue.com/haskell-in-c-sharp-functors/">&lt;p&gt;Look out; I’ve been digging into Haskell again. I enjoy learning about Haskell, but more than that, I enjoy learning to think in a functional way. Haskell is a particularly good language to facilitate that, as it is arguably the most functional of functional languages. It boasts “pure” functionality, and while some might believe that means it is of no practical use, I think it makes it pretty cool.&lt;/p&gt;

&lt;p&gt;While I enjoy my Haskell studies, I still struggle with a few core concepts. For example, I keep having to go back and reread explanations of Functors. I understand them at the time, but it doesn’t stick, and I keep having to come back and read it again. Maybe my appreciation for some of the staples of Haskell would be a little less volatile if I had the experience of implementing them myself in an imperative language like C#.&lt;/p&gt;

&lt;p&gt;To implement Functors, there are a few building blocks we’ll need first, and it will be helpful to establish a simple concept map between pieces of Haskell and C#. Let’s start with one of the foundational constructs of the functional world, the &lt;em&gt;map&lt;/em&gt; function. The type of Haskell’s &lt;em&gt;map&lt;/em&gt; function is defined like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This means that the &lt;em&gt;map&lt;/em&gt; function takes a function that takes a value of type &lt;em&gt;a&lt;/em&gt; and returns a value of type &lt;em&gt;b&lt;/em&gt;, and a list of values of type &lt;em&gt;a&lt;/em&gt;, and returns a list of values of type &lt;em&gt;b&lt;/em&gt;. In fewer words, it takes a function from &lt;em&gt;a&lt;/em&gt; to &lt;em&gt;b&lt;/em&gt;, and a list of &lt;em&gt;a&lt;/em&gt;, and returns a list of &lt;em&gt;b&lt;/em&gt;. It takes a function and a list of inputs to it, and returns a list of outputs. You’ve probably seen this kind of thing in other languages. I think I first encountered it in Ruby. Shortly thereafter I was thrilled to see that LINQ had brought this goodness to .NET in the form of the &lt;em&gt;Select&lt;/em&gt; extension method on &lt;em&gt;IEnumerable&lt;/em&gt;. Of course the LINQ implementation uses the very OO approach of adding an extension method onto the &lt;em&gt;IEnumerable&lt;/em&gt; type. Its signature looks like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;selector&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Stare at the two for a minute if you need to; the Haskell type and the C# method signature say exactly the same thing, only stated in reverse order from one another. (And C# takes a heck of a lot more notes to name that tune.)&lt;/p&gt;

&lt;p&gt;So the two languages have some common ground to start with. Now we need to get a little bit of Haskell vocabulary under our belts. I will try to describe Haskell constructs in terms of C# constructs as much as possible.&lt;/p&gt;
&lt;h3&gt;Type Classes&lt;/h3&gt;

&lt;p&gt;Try to ignore the word &lt;em&gt;class&lt;/em&gt;; it’s not like an OO class. A type class in Haskell is closer to an interface in OO. Types can be instances of various type classes much like classes in C# can implement various interfaces. A type class comprises a list of functions, and any type that wants to be an instance of that type class must implement its functions. It might not be too much of a stretch to say it &lt;em&gt;is&lt;/em&gt; interface implementation. Each instance type has its own unique implementation of each function, just as each implementing class has its own unique implementation of an interface’s members, affording the system the same benefits of polymorphism that we have always enjoyed in OO.&lt;/p&gt;
&lt;h3&gt;Type Constructors&lt;/h3&gt;

&lt;p&gt;You can think of a type constructor as something like a generic type in .NET, such as &lt;em&gt;List&amp;lt;T&amp;gt;&lt;/em&gt; or &lt;em&gt;Nullable&amp;lt;T&amp;gt;&lt;/em&gt;. It can’t stand alone as a type; it needs another type to make it complete. You can’t instantiate a &lt;em&gt;List&amp;lt;T&amp;gt;&lt;/em&gt; unless the identifier &lt;em&gt;T&lt;/em&gt; is bound to an actual type such as &lt;em&gt;int&lt;/em&gt; or &lt;em&gt;string&lt;/em&gt;. Type constructors work the same way.&lt;/p&gt;
&lt;h3&gt;Functors&lt;/h3&gt;

&lt;p&gt;Haskell’s &lt;em&gt;Functor&lt;/em&gt; is a particular type class, and it’s pretty simple as type classes go, in that it has only one function, &lt;em&gt;fmap&lt;/em&gt;, but it wields some serious power. &lt;em&gt;fmap&lt;/em&gt;’s type is defined like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This means that, given that a type constructor &lt;em&gt;f&lt;/em&gt; is a functor, &lt;em&gt;fmap&lt;/em&gt; takes a function that takes an &lt;em&gt;a&lt;/em&gt; and returns a &lt;em&gt;b&lt;/em&gt;, and an &lt;em&gt;f&lt;/em&gt; of &lt;em&gt;a&lt;/em&gt;, and returns an &lt;em&gt;f&lt;/em&gt; of &lt;em&gt;b&lt;/em&gt;. We could express this signature in C# like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It is commonly stated that functors are structures than can be “mapped over.” They establish a context that contains a particular type of value, and then they can take a function, apply it to the contained value, and wrap up the result in the same type of context again, returning the new context as the result. Think of the &lt;em&gt;map&lt;/em&gt; function we just looked at. It maps functions over lists. It pulls a value out of a list, applies a function to it, and puts the result into a list. Hey, if lists can be mapped over, does this mean that lists are functors? Why, yes, it does. Take another look at the &lt;em&gt;fmap&lt;/em&gt; type definition above and think of &lt;em&gt;f&lt;/em&gt; as a context. &lt;em&gt;f a&lt;/em&gt; represents an &lt;em&gt;a&lt;/em&gt; in this context. &lt;em&gt;f b&lt;/em&gt; represents a &lt;em&gt;b&lt;/em&gt; in the same type of context. Now imagine that context is a list, and therefore that &lt;em&gt;f a&lt;/em&gt; is a list of &lt;em&gt;a&lt;/em&gt;. As it turns out, &lt;em&gt;map&lt;/em&gt; is the list’s implementation of &lt;em&gt;fmap&lt;/em&gt; that makes it a Functor instance. Nice!&lt;/p&gt;
&lt;h3&gt;A C# Implementation&lt;/h3&gt;

&lt;p&gt;But can we do this in C#? The &lt;em&gt;Select&lt;/em&gt; method uses generic type variables to give its parameters the same flexibility that Haskell’s &lt;em&gt;map&lt;/em&gt; function has. It uses &lt;em&gt;IEnumerable&lt;/em&gt; as its context in place of Haskell’s list. Now we’re looking to define a new generic type that could take the place of &lt;em&gt;IEnumerable&lt;/em&gt;. If different functor types have different implementations of &lt;em&gt;fmap&lt;/em&gt;, we’re headed for some polymorphism here. If we’re going to implement &lt;em&gt;fmap&lt;/em&gt; polymorphically in C# we’ll need an interface and at least one class. Since we’re going OO here, we’ll eliminate the input parameter we included before and replace it with a self reference (this) in the implementation.&lt;/p&gt;

&lt;p&gt;First the interface:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Simple enough. Now for a class to implement it. Let’s try a list. It will implement IEnumerable&lt;TSource&gt; for its list-ness, and IFunctor&lt;TSource&gt; for its functor-ness.&lt;/TSource&gt;&lt;/TSource&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now for a bare-bones, boilerplate implementation of IEnumerable&lt;TSource&gt;, we'll just take an IEnumerable&lt;TSource&gt; in the constructor and delegate any IEnumerable concerns to it.&lt;/TSource&gt;&lt;/TSource&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_collection&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;IEnumerator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetEnumerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Thanks to &lt;em&gt;System.Linq&lt;/em&gt; and the &lt;em&gt;Select&lt;/em&gt; extension, &lt;em&gt;MyEnumerable&lt;/em&gt; already has the ability to map functions over itself, but it can’t join in any functor games until it implements &lt;em&gt;IFunctor&lt;/em&gt;’s &lt;em&gt;FMap&lt;/em&gt;. Fortunately, we can have &lt;em&gt;FMap&lt;/em&gt; delegate to &lt;em&gt;Select&lt;/em&gt;, since it already does the same thing. &lt;em&gt;Select&lt;/em&gt; takes the value from the context (&lt;em&gt;MyEnumerable&lt;/em&gt;) and applies the function, but it’s still up to us to put the result back into context, which we can easily do by passing it to &lt;em&gt;MyEnumerable&lt;/em&gt;’s constructor.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There’s the &lt;em&gt;MyEnumerable&lt;/em&gt;-specific implementation. Now let’s just add the explicit interface implementation and have it delegate to the method we just added.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That’s it. We have implemented a functor in C#. Let’s try it out before we move on. Say we have a function that takes an integer and returns a string of its square.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SquareString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And we have a list of integers that we want to map this function over.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;MyEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;MyEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;squareStrings&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SquareString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//=&amp;gt; {&quot;1&quot;, &quot;4&quot;, &quot;9&quot;, &quot;16&quot;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Of course, in C# we tend to love lambdas, and we could just as easily do it that way.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;squareStrings&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//=&amp;gt; {&quot;1&quot;, &quot;4&quot;, &quot;9&quot;, &quot;16&quot;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;But wait a minute. We haven’t done anything but wrap &lt;em&gt;Select&lt;/em&gt;, have we? So far, yes, but think about this: What if the context we want to map over is not a listy kind of thing? Another functor type in Haskell is &lt;em&gt;Maybe a&lt;/em&gt;, which is just like .NET’s &lt;em&gt;Nullable&amp;lt;T&amp;gt;&lt;/em&gt;. It has a particular type, and it may or may not have a value. Just like a list, it provides a context for particular type of value, only in this case the context is the possibility of there being no value. Let’s throw together our own homegrown nullable class.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The guts are pretty simple.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_hasValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_hasValue&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HasValue&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_hasValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_hasValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InvalidOperationException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;s&quot;&gt;&quot;Nullable object must have a value.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now for the FMap implementation. Once again, we take the value from the context, apply the function, and wrap it up in context. In this case, if the MyNullable&amp;lt;TSource&amp;gt; instance has a value, we apply the function and put the result into a new MyNullable&amp;lt;TResult&amp;gt;. If there is no value, we simply return a new empty MyNullable&amp;lt;TResult&amp;gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HasValue&lt;/span&gt; 
            &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
            &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IFunctor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now let’s see what MyNullable can do.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nullInt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// nullInt.HasValue == false&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;three&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyNullable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// three.HasValue == true&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// three.Value == 3&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ss3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;three&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SquareString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ss3.HasValue == true&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ss3.Value == &quot;9&quot;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssNull&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nullInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SquareString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ssNull.HasValue = false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Okay, so we have implemented Haskell’s Functor in C#. What is the value in this? Remember the goal of this exercise was not to bring C# something that I thought it needed, but rather to help me understand what Functors are doing for us in Haskell. What I find here is that it’s all about separation of concerns. The benefit of the separation is in the reusability of the functions that we pass to &lt;em&gt;fmap&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Each Functor, or type context, has its own set of concerns that are not the business of any function we might want to map over it. The list and &lt;em&gt;MyEnumerable&lt;/em&gt; need to iterate, call the specified function multiple times, and collect the results into a new list. The &lt;em&gt;Maybe&lt;/em&gt; and &lt;em&gt;MyNullable&lt;/em&gt; need to deal with the absence of a value and possibly not call the specified function at all. This Functor mechanism enables simple functions such as &lt;em&gt;SquareString&lt;/em&gt; to be reused in these and limitless other contexts that they are unaware of. Each Functor context can take any simple function, and each simple function can be used in any Functor context. Without the Functor, we would have to code each function-context combination separately. With it, we have a pallette of primary colors that can be combined in unforeseen and beautiful ways.&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="haskell" /><category term="functional" /><category term="c#" /><summary type="html">Look out; I’ve been digging into Haskell again. I enjoy learning about Haskell, but more than that, I enjoy learning to think in a functional way. Haskell is a particularly good language to facilitate that, as it is arguably the most functional of functional languages. It boasts “pure” functionality, and while some might believe that means it is of no practical use, I think it makes it pretty cool.</summary></entry><entry><title type="html">Polyglot Survival Guide: Erlang, Haskell, F#, and Elixir</title><link href="https://www.codefugue.com/polyglot-survival-guide-erlang-and-haskell/" rel="alternate" type="text/html" title="Polyglot Survival Guide: Erlang, Haskell, F#, and Elixir" /><published>2014-01-20T15:43:00+00:00</published><updated>2014-01-20T15:43:00+00:00</updated><id>https://www.codefugue.com/polyglot-survival-guide-erlang-and-haskell</id><content type="html" xml:base="https://www.codefugue.com/polyglot-survival-guide-erlang-and-haskell/">&lt;p&gt;Lately I have been bouncing back and forth between Erlang and Haskell, two profoundly significant functional languages that I find both very different and very similar. They are very different in their philosophies and thought patterns, particularly around data types, but both being non-LISPy functional languages, they have enough similarities that I sometimes having trouble keeping them straight when I am coding. So I have started a simple list here cataloging some of their differences side by side. I imagine I will continue to add to it over time. Please comment to let me know what other features or constructs you might like to see added here.&lt;/p&gt;

&lt;p&gt;I have now added entries for F# and Elixir, which I am also working with lately. While Elixir is the least adopted so far, I put it right after Erlang, since their relationship leads to a lot of similarities in the details cataloged here.&lt;/p&gt;
&lt;h5&gt;Functional Purity&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;no&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;no&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;yes (in that all side effects are relegated to the IO monad)&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;no&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;REPL&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;erl&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;iex&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;ghci&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;fsi&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Exiting the REPL&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;q().&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;ctrl-c ctrl-c&lt;/pre&gt;
or
&lt;pre&gt;ctrl-g q&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;:q&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;#quit;;&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Line terminators&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;English-like (, ; .)&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;none&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;none&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;none&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Typing&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;dynamic&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;dynamic&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;static with inference&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;static with inference&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;I/O&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;io, file, and some other modules&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;IO, File, and some other modules&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;IO monad&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;.NET Framework&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Printing&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;io:format(&quot;Hello~n&quot;).&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;IO.puts &quot;Hello\n&quot;&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;putStrLn &quot;Hello&quot;&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;printfn &quot;Hello&quot;&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Variable names&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;PascalCase&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;snake_case&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;camelCase&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;camelCase&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Atoms&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;atom&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;:atom&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;(none)&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;(none)&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Laziness&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;hackable with funs&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;streams lazy, lists not&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;by default&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;sequences lazy, lists not; explicit with &lt;em&gt;lazy&lt;/em&gt; keyword&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Infinite lists&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;hackable with funs&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;streams, yes; lists, no&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;absolutely&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;sequences with &lt;em&gt;yield&lt;/em&gt;&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Currying&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;explicit with lambdas, no dedicated syntax&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;explicit with lambdas, no dedicated syntax&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;implicit&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;implicit&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Side effects&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;yes&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;yes&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;never, except via the IO monad&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;yes&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Tuples&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;{&quot;foo&quot;, &quot;bar&quot;}&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;{&quot;foo&quot;, &quot;bar&quot;}&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;(&quot;foo&quot;, &quot;bar&quot;)&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;&quot;foo&quot;, &quot;bar&quot;&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;List construction/matching&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;[Head|Tail]&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;[h|t]&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;x:xs&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;x :: xs&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Heterogeneous lists&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;yes&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;yes&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;no&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;no&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Function calls&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;add(1,2)&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;add(1,2)&lt;/pre&gt;
or
&lt;pre&gt;add 1,2&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;add 1 2&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;add 1 2&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Lambdas&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;fun(X,Y) -&amp;gt; X + Y end&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;fn x,y -&amp;gt; x + y end&lt;/pre&gt;
or
&lt;pre&gt;&amp;amp;(&amp;amp;1 + &amp;amp;2)&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;\x y -&amp;gt; x + y&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;fun x y -&amp;gt; x + y&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;List comprehensions&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;[X || X &amp;lt;- [1,2,3,4,5], X &amp;gt; 3]&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;for x &amp;lt;- [1,2,3,4,5], x &amp;gt; 3, do: x&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;[x | x &amp;lt;- [1,2,3,4,5], x &amp;gt; 3]&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;{ for x in 1 .. 5 when x &amp;gt; 3 -&amp;gt; x }&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Converting integer to string&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;integer_to_list(123)&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;to_string 123&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;show 123&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;string 123&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Comments&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;% comment after percent&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;# comment after pound sign&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;-- comment after double hyphen&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;// comment after double slash&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;h5&gt;Mapping&lt;/h5&gt;
&lt;dl&gt;&lt;dt&gt;Erlang&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;lists:map(fun(X) -&amp;gt; X*X end, [1,2,3]).&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Elixir&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;Enum.map [1,2,3], &amp;amp;(&amp;amp;1 * &amp;amp;1)&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;Haskell&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;map (x -&amp;gt; x*x) [1..3]&lt;/pre&gt;
&lt;/dd&gt;&lt;dt&gt;F#&lt;/dt&gt;&lt;dd&gt;
&lt;pre&gt;List.map (fun x -&amp;gt; x*x) [1 .. 3]&lt;/pre&gt;
&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt; &lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="haskell" /><category term="functional" /><category term="f#" /><category term="erlang" /><category term="elixir" /><summary type="html">Lately I have been bouncing back and forth between Erlang and Haskell, two profoundly significant functional languages that I find both very different and very similar. They are very different in their philosophies and thought patterns, particularly around data types, but both being non-LISPy functional languages, they have enough similarities that I sometimes having trouble keeping them straight when I am coding. So I have started a simple list here cataloging some of their differences side by side. I imagine I will continue to add to it over time. Please comment to let me know what other features or constructs you might like to see added here.</summary></entry><entry><title type="html">Elixir: Language Love Child?</title><link href="https://www.codefugue.com/elixir-language-love-child/" rel="alternate" type="text/html" title="Elixir: Language Love Child?" /><published>2013-04-28T21:04:00+00:00</published><updated>2013-04-28T21:04:00+00:00</updated><id>https://www.codefugue.com/elixir-language-love-child</id><content type="html" xml:base="https://www.codefugue.com/elixir-language-love-child/">&lt;p&gt;Different programming languages, like different people, have their strengths and weaknesses. Some are simple; some are complex. Some are friendly to the programmer; some are friendly to the processor. Some cater to brilliant minds; some cater to other minds. I have spent a significant portion of my development career writing code in C#, which is accurately described as a general purpose language. It aims to be able to do most anything, rather than to be the best at any one particular thing.&lt;/p&gt;

&lt;p&gt;After several years of living mostly in C#, I ran across Ruby and fell in love with it. It’s no surprise, because Ruby was designed to optimize for developer enjoyment. It’s very readable with low ceremony, and it takes no time to get something up and running. The down side is that it is not fast in execution. It’s one of the slowest languages in use today, but when I’m writing Ruby, I don’t care. I write in Ruby when speed of development is more important than speed of execution, or when I just want to have fun.&lt;/p&gt;

&lt;p&gt;Lately I’ve been dabbling in Erlang, which I would put at the other end of the spectrum from Ruby. Erlang is a generation-old functional language that is all about concurrency and fault tolerance. Its runtime, called OTP (Open Telecom Platform), does a shockingly good job of solving a lot of the concurrency and uptime problems facing the software world in recent years. The Erlang language is clearly tailored to the needs of a concurrent runtime, and it achieves this goal brilliantly. One down side is the syntax. Erlang’s syntax is unusual, and it is not often described as pretty. Even among real lovers of Erlang, I haven’t heard anyone defend its syntax. We just accept the syntax because we’re more concerned with concurrency and reliability.&lt;/p&gt;

&lt;p&gt;But is it ever possible to have our cake and eat it, too? What if we were so demanding as to ask for the beauty of Ruby’s syntax and the concurrency magic of Erlang on the same platform? Enter &lt;a title=&quot;Elixir&quot; href=&quot;http://elixir-lang.org/&quot;&gt;Elixir&lt;/a&gt;. Worlds are colliding. Elixir is a language that runs on the Erlang runtime but sports a syntax that looks an awful lot like Ruby. The best of both worlds? Could be. I’m just now gearing up to dig in and see. Elixir, I’m rooting for you.&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="erlang" /><category term="ruby" /><category term="elixir" /><summary type="html">Different programming languages, like different people, have their strengths and weaknesses. Some are simple; some are complex. Some are friendly to the programmer; some are friendly to the processor. Some cater to brilliant minds; some cater to other minds. I have spent a significant portion of my development career writing code in C#, which is accurately described as a general purpose language. It aims to be able to do most anything, rather than to be the best at any one particular thing.</summary></entry><entry><title type="html">Moops! Haskell, IO, and the Bubble Boy</title><link href="https://www.codefugue.com/moops-haskell-io-and-the-bubble-boy/" rel="alternate" type="text/html" title="Moops! Haskell, IO, and the Bubble Boy" /><published>2012-05-28T13:53:00+00:00</published><updated>2012-05-28T13:53:00+00:00</updated><id>https://www.codefugue.com/moops-haskell-io-and-the-bubble-boy</id><content type="html" xml:base="https://www.codefugue.com/moops-haskell-io-and-the-bubble-boy/">&lt;p&gt;I recently set out to get Haskell connected to a PostgreSQL database. The task ended up having almost nothing to do with PostgreSQL and everything to do with Haskell’s militant stance on functional purity. I chose to use HDBC since it appeared to be the most vanilla approach. Before I even got to the point of trying to open a database connection, I was confronted with a philosophical conundrum not too different from the sound of one hand clapping.&lt;/p&gt;

&lt;p&gt;I’ll provide some code here in case you want to follow along in ghci (the Glasgow Haskell Compiler Interactive), but I’m really more about the philosophy here. First I imported the HDBC library and the PostgreSQL driver.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;ghci&amp;gt; import Database.HDBC
ghci&amp;gt; import Database.HDBC.PostgreSQL&lt;/pre&gt;
&lt;p&gt;Next, I see that the library has a function called connectPostgreSQL whose job is to open a connection for me. I have always admired Haskell’s commitment to functional purity, even though I have wondered sometimes whether that rendered it useless in real life. Haskell functions operate under the same constraints as mathematical functions. According to Wikipedia, in mathematics, a &lt;a href=&quot;http://en.wikipedia.org/wiki/Function_(mathematics)&quot; target=&quot;_blank&quot;&gt;function&lt;/a&gt; is, “a relation between a set of inputs and a set of potential outputs with the property that each input is related to exactly one output.” A function, given a particular input, will always return the same result. Pure functional programming adds the constraint that a function will have no side effects.&lt;/p&gt;

&lt;p&gt;I have connected several programming languages to databases, and they have mostly worked in the same general way. You call a function or method with some basic connection information, it opens a connection, and you get back an object reference or a handle that can subsequently be used to interact with the connection that has been opened. How will this work in a pure functional language? Opening a database connection is a side effect in the first place, so we’re already breaking the rules, and we haven’t even done anything yet.&lt;/p&gt;

&lt;p&gt;Let’s ignore that for the moment and think about what the function’s result should be. What type of value is this function going to give me? Once I have my connection details defined and pass them to this function, it should return me a handle to an open database connection, right? Well, it will if the connection information is correct, and the server is running, and I have a network connection to it, and my password hasn’t expired, etc. This exact function call might give me a connection one time, but then I can turn off my wireless and call the function again, and I will get a very different result. Mathematics would tell me this is not a function. Haskell would agree.&lt;/p&gt;

&lt;p&gt;This whole operation of opening a database connection is tainted. It is not a mathematical operation. It is not a functional operation. It is imperative by nature. It is entirely side-effect driven. It is not about getting an answer; it is about changing something in the world. Haskell does not take this lightly. It does not prevent you from doing it, but it does not allow such tainted operations to pollute its pristine functions. Such imperative, side-effect-riddled operations are relegated to a language feature known as the IO action. (The IO action is technically a monad, but that’s a topic for another day.) The connectPostgreSQL function by definition cannot return me a handle to a connection that it may or may not be able to establish, but it can reliably return me an IO action that by definition behaves imperatively and unpredictably. This IO action is our window into the cruel outside world. It is our bubble.&lt;/p&gt;

&lt;p&gt;Haskell is the bubble boy. In his world there are no germs, no file systems, no databases, no spotty networks, only pure math. The IO action is his window to the outside world, his bubble. He can see through it, talk through it, and use those hose sleeves and rubber gloves to play Trivial Pursuit through it, but he can never actually touch anything. If did, he would be contaminated, infected, tainted, impure. The bubble keeps him safely insulated from all the brutal unpredictability of the outside world.&lt;/p&gt;

&lt;p&gt;So the connectPostgreSQL function can’t return me a database connection, but it can predictably and consistently return me an IO action, which is by definition tainted, impure, unpredictable, and unreliable, and this IO action just might, in the perfect alignment of the stars and all favorable circumstances, contain a database connection for me. We can ask ghci for the type of the function like this:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;ghci&amp;gt; :type connectPostgreSQL
connectPostgreSQL :: String -&amp;gt; IO Connection&lt;/pre&gt;
&lt;p&gt;Here we see that it takes a String (providing connection details) and returns an IO action that might give us a Connection. Once again, this function will always succeed. The perils of the impurity of the outside world pose us no threat until we ask the IO action for the connection. We can do this with the &amp;lt;- operator.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;ghci&amp;gt; let io = connectPostgreSQL &quot;dbname=mydb password=secret&quot;
ghci&amp;gt; :type io
io :: IO Connection
ghci&amp;gt; -- Here's the scary part:
ghci&amp;gt; conn &amp;lt;- io
ghci&amp;gt; -- Hey, it worked!
ghci&amp;gt; :type conn
conn :: Connection&lt;/pre&gt;
&lt;p&gt;If we have made it this far, we now have a connection handle that we can use to execute queries. There’s a function here called quickQuery for doing just that. Of course, the connection could be severed at any time by forces entirely beyond our control. Even if the connection could be reliable, the same select query may very well yield a different result every time I execute it. That’s kind of the point of having a database, right? I think you know what I’m gettting at here. If the operation’s result is unpredictable, it can’t be a function.&lt;/p&gt;

&lt;p&gt;Once again, IO action to the rescue. quickQuery can still be a function if it predictably returns an IO action instead of a surprise from some flaky database.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;ghci&amp;gt; :type quickQuery
quickQuery
  :: IConnection conn =&amp;gt;
     conn -&amp;gt; String -&amp;gt; [SqlValue] -&amp;gt; IO [[SqlValue]]&lt;/pre&gt;
&lt;p&gt;This tells us that quickQuery takes a conn (which must be some type of IConnection), a string (for the query), and a list of SqlValues (for proper query parameters to avoid SQL injection attacks), and it returns us an IO action. This IO action, however, may contain in its tainted wildness not a database connection but a list of lists of SqlValues. Of course the outer list can be seen as list of rows, and the inner lists as lists of column values.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;ghci&amp;gt; let resultIO = quickQuery conn &quot;select count(*) from some_table&quot; []
ghci&amp;gt; :type resultIO
resultIO :: IO [[SqlValue]]
ghci&amp;gt; --That was safe: now the scary part:
ghci&amp;gt; result &amp;lt;- resultIO
ghci&amp;gt; --Whew, got lucky again.
ghci&amp;gt; :type result
result :: [[SqlValue]]
ghci&amp;gt; result
[[SqlInteger 4]]&lt;/pre&gt;
&lt;p&gt;At this moment we can see that some_table has 4 rows in it. When I make that exact function call again, it might have 5, but it doesn’t compromise the purity of the function because that number is not the function’s result. That number is the result of executing an IO action, which does not pretend to have the determinism of a function. The pure function has the IO action do its dirty work.&lt;/p&gt;

&lt;p&gt;Life is unpredictable, but mathematics is not. The IO action gives Haskell the means to exploit the predictability of its mathematical bias but still interact with the unpredictable outside world, thereby making it, well, useful.&lt;/p&gt;</content><author><name>Calvin Bottoms</name></author><category term="haskell" /><category term="functional" /><summary type="html">I recently set out to get Haskell connected to a PostgreSQL database. The task ended up having almost nothing to do with PostgreSQL and everything to do with Haskell’s militant stance on functional purity. I chose to use HDBC since it appeared to be the most vanilla approach. Before I even got to the point of trying to open a database connection, I was confronted with a philosophical conundrum not too different from the sound of one hand clapping.</summary></entry></feed>