Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Noah Hradek
Noah Hradek

Posted on

Monads for Great Fun

One of my favorite books on Haskell is Learn You a Haskell by Miran Lipovača. Often monads are interpreted in a mathematical or purely functional context, and this is the approach in Learn You a Haskell. However, their reach goes far beyond that. In this tutorial, I won't entirely explain what monads are; I assume you already have a decent knowledge of this. If not, read the monad section of the book or read another tutorial like on theHaskell site.

Instead, I will show you what monadic computation is capable of in terms of various interesting applications. To briefly review, a monad is a computational context with an operator bind (>>=) which takes in a monadic value then applies a function to the unwrapped value and returns another monad. It's like a wrapper that wraps another value with context. Looking at the type signature you can see what I mean.

(>>=)::ma->(a->mb)->mb
Enter fullscreen modeExit fullscreen mode

Another operator (>>) does the same thing but without any input. This operator merely chains together two monads without any function applied.

(>>)::ma->mb->mb
Enter fullscreen modeExit fullscreen mode

The do syntax just does (>>) repeatedly without needing to write the operator explicitly. For example we could write the following code.

doSomething=Just2>>Just3>>Nothing
Enter fullscreen modeExit fullscreen mode

In this do form.

doSomething=doJust2Just3Nothing
Enter fullscreen modeExit fullscreen mode

We can use the (<-) operator to unwrap the monadic value and return a normal unwrapped value. For example, getting a line of text from an IO monad and printing the value.

main=doln<-getLineputStrLnln
Enter fullscreen modeExit fullscreen mode

But there are some there example where Monads might be very useful. I'll go over a few of these.

HTTP

One example would be for HTTP authentication passing. I'm not familiar if existing frameworks like Yesod implement this or not. However, we could do something like this to store an authentication context instead of having to pass it each time as a header.

getFromWeb::HTTP()getFromWeb=doauthentication"Bearer: TOKEN"res1<-get"http://someurl.com/api/get"post"http://someurl.com/api/post""{JSON data}"
Enter fullscreen modeExit fullscreen mode

The authentication function would return a HTTP monad with the authentication context of a bearer token.

Graphics

Often times drawing is implemented as a complex state machine with many parameters to keep track of. For example in OpenGL you have to set the viewport then the vertex arrays and so on. In Processing and Javascript with the canvas you have to do something similar like this.

constctx=canvas.getContext("2d");ctx.beginPath();ctx.moveTo(50,140);ctx.lineTo(150,60);ctx.lineTo(250,140);ctx.closePath();ctx.stroke();
Enter fullscreen modeExit fullscreen mode

This is the perfect example to "monadize." We could write something similar using a path monad like this.

stroke::Context2D->Path->DrawIOcreateShape::PathcreateShape=domoveTo50140lineTo15060lineTo250140main::DrawIOmain=dostroke$(createContext"2d")createShape
Enter fullscreen modeExit fullscreen mode

No more needing to keep track of where the path begins and ends and we no longer need to keep track of the drawing context.

Sound

I was thinking of applying monads to signal processing for audio. Let's say we have a signal monad which carries context about the signal. Imagine we took in an input signal and passed it through a low-pass-filter at 440hz and then a delay line of 0.5ms, it might look something like this with binds.

filterAndDelay::Signal->SignalfilterAndDelayinput=input>>=lpf440>>=delay0.5
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Python programmer working on backend Django development, with an interest in machine learning, graphics, and sound.
  • Education
    BSc Computer Science
  • Work
    Junior Software Engineer
  • Joined

More fromNoah Hradek

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp