Skip to content

Instantly share code, notes, and snippets.

@cscalfani
cscalfani / DiscoveringApplicative.md
Created December 27, 2017 02:23
Discovering Applicative Functors in Haskell

Discovering Applicative Functors in Haskell

When first learning Applicatives, they always seemed an oddball thing to me. At first, Functors made sense and Monads sort of made sense, but Applicatives felt shoehorned in between the two.

That is until I was reading Programming In Haskell by Graham Hutton, Second Edition (section 12.2) on Applicatives. His explanation takes a bit of a leap and is presented in an odd order, but I think I can reorder the information in such a way that we can feel as if we are discovering Applicative Functors (or Applicatives for short) ourselves.

Start with Functor

Let's remind ourselves of the Functor class definition:

(Compose) Compose (Compose)

Motivation

Multiple times I've wanted to understand how to derive the following by hand:

(.) . (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d

Which is the point free version of:

@cscalfani
cscalfani / mapAllWrong.md
Last active April 28, 2021 09:33
You're thinking about map all wrong
@cscalfani
cscalfani / ThinkAboutMonads.md
Last active December 4, 2022 20:58
How to think about monads

How to think about Monads

UPDATE 2021: I wrote this long before I wrote my book Functional Programming Made Easier: A Step-by-step Guide. For a much more in depth discussion on Monads see Chapter 18.

Initially, Monads are the biggest, scariest thing about Functional Programming and especially Haskell. I've used monads for quite some time now, but I didn't have a very good model for what they really are. I read Philip Wadler's paper Monads for functional programming and I still didnt quite see the pattern.

It wasn't until I read the blog post You Could Have Invented Monads! (And Maybe You Already Have.) that I started to see things more clearly.

This is a distillation of those works and most likely an oversimplification in an attempt to make things easier to understand. Nuance can come later. What we need when first le

@cscalfani
cscalfani / FuncApplicationHaskell.md
Created June 9, 2018 18:44
Fun with Function Application (Haskell version)

Fun with Function Application (Haskell version)

Function application operators in Haskell make programming more understandable and help reduce parenthesis.

($)

The application operator, $, can be used to reduce the need for parenthesis. The following:

@cscalfani
cscalfani / FuncApplicationElm.md
Last active June 11, 2018 22:59
Fun with Function Application (Elm version)

Fun with Function Application (Elm version)

Function application operators in Elm make programming more understandable and help reduce parenthesis.

(<|)

The application operator, <|, can be used to reduce the need for parenthesis. The following:

foldr written the normal way and as the dual of unfoldr

The following code was inspired by Kwang's Haskell Blog post titled unfold and fold.

module Main where

import Prelude hiding (foldr)
import Data.Function ((&))

How to Think About Monads 2

How to Think about Monads, starts with composition of pure functions and quickly transitioned to composition of pure functions with side-effects. If you haven't read it yet, please do. This continues where that left off.

By the end of that article, we created specialized composition functions that were equivalent to the Monadic bind.

From that article we can conclude that Monadic computations have the following properties:

  1. Functions are executed sequentially
  2. Optionally, control-flow can be short-circuited
@cscalfani
cscalfani / ElmVSPurescript.pdf
Last active August 27, 2021 15:03
Here are the slides to the first lecture in my training class I'm running at work for the Elm programmers to help them transition to PureScript.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.