in Code

Recent Entries (Page 9)

  • The List MonadPlus --- Practical Fun with Monads (Part 2 of 3)

    Part two of an exploration of a very useful design pattern in Haskell known as MonadPlus, a part of an effort to make “practical” monads less of a mystery and fun to the good peoples of this earth.

    When we last left off on the MonadPlus introduction, we understood that there are times when you want to chain functions on objects in a way that “resembles” a failure/success process. We did this by exploring the most simple of all MonadPlus’s: a simple “dumb” container for a value is either in a success or a failure. We looked at how the MonadPlus design pattern really “behaved”.

    This time we’re going to look at another MonadPlus — the List. By the end of this series we’re going to be using nothing but the list’s MonadPlus properties to solve this classic logic problem:

    A farmer has a wolf, a goat, and a cabbage that he wishes to transport across a river. Unfortunately, his boat can carry only one thing at a time with him. He can’t leave the wolf alone with the goat, or the wolf will eat the goat. He can’t leave the goat alone with the cabbage, or the goat will eat the cabbage. How can he properly transport his belongings to the other side one at a time, without any disasters?

    Let’s get to it!

    Read more … Comments

  • Practical Fun with Monads --- Introducing: MonadPlus!

    Monads. Haskell’s famous for them, but they are one of the most ill-understood concepts to the public. They are mostly shrouded in mystery because of their association with how Haskell models I/O. This reputation is undeserved. Monads don’t have anything to do with I/O.

    This series is a part of a global effort to pull away the shroud of mystery behind monads and show that they are fun! And exciting! And really just a way of chaining together functions that allow for new ways of approaching puzzles.

    The first sub-series (chapter?) will be on a specific class/family of monads known as MonadPlus. At the end of it all, we are going to be solving the classic logic puzzle, as old as time itself, using only the List monad instance, and no loops, queues, or fancy stuff like that:

    A farmer has a wolf, a goat, and a cabbage that he wishes to transport across a river. Unfortunately, his boat can carry only one thing at a time with him. He can’t leave the wolf alone with the goat, or the wolf will eat the goat. He can’t leave the goat alone with the cabbage, or the goat will eat the cabbage. How can he properly transport his belongings to the other side one at a time, without any disasters?

    Let us enter a brave new world!

    Read more … Comments

  • A Brief Primer on Classical and Quantum Mechanics for Numerical Techniques

    Okay! In this series we will be going over many subjects in both physics and computational techniques, including the Lagrangian formulation of classical mechanics, basic principles of quantum mechanics, the Path Integral formulation of quantum mechanics, the Metropolis-Hastings Monte Carlo method, dealing with entropy and randomness in a pure language, and general principles in numerical computation! Fun stuff, right?

    The end product will be a tool for deriving the ground state probability distribution of arbitrary quantum systems, which is somewhat of a big deal in any field that runs into quantum effects (which is basically every modern field). But the real goal will be to hopefully impart some insight that can be applied to broarder and more abstract applications. I am confident that these techniques can be applied to many problems in computation to great results.

    I’m going to assume little to no knowledge in Physics and a somewhat intermediate working knowledge of programming. We’re going to be working in both my favorite imperative language and my favorite functional language.

    In this first post I’m just going to go over the basics of the physics before we dive into the simulation. Here we go!

    Read more … Comments

  • The Compromiseless Reconciliation of I/O and Purity

    One of the crazy ideals of functional programming is the idea that your program is simply a list of definitions of mathematical functions. And like real math functions, FP functions are pure. That means that (1) they cannot affect any state, and (2) that they must return the same thing every time they are called with the same arguments.

    When you first learn functional programming, this manifests as “your variables are immutable and you can’t do loops; use recursion instead.” And if you do that, everything is “fine”.

    However, there is an apparent glaring problem with this adherence to purity: I/O. Input and output are inherently stateful.

    Read more … Comments

  • log.sh: Lightweight Command Line Note & Logging

    What do you use to send off quick one-off notes and logs about a project you are working on? Found a nice link to a resource you’ll want to look up later…want to jot down a sudden realization?

    Maybe you use some external note-taking software, like Evernote. But wouldn’t it be nice to have something that is completely in the command line? Do you really need to fire up an entire GUI just to write down one line, put down one link? And do you really need these notes to all be thrown in with your others?

    You might be using a command line interface to a larger note-taking system like geeknote. But it’s kind of a hassle to open up an entire text editor every time you want to make a small one-liner. Doesn’t quite meld with the Unix philosophy. Maybe you are comfortable simply appending to a text file with >>…but what if you want to add things like timestamps?

    Here’s introducing log.sh.

    Read more … Comments

  • Deploying Medium to Large Haskell Apps to Heroku by Precompiling

    UPDATE: This post was written in 2013, where the options available to someone looking to host a Haskell site on Heroku were fairly limited. It’s (as of the time of writing this) 2015 now and things have changed. Check out the comments for two good alternatives to this that are working today!

    Consider the rest of this article obsolete, or look here if none of the solutions given in the comments work :)

    Read more … Comments

  • The Hamster Hotel: An Introduction to Control Theory (Part 2)

    As we left it off in part 1, our elevator is still in trouble. You have an elevator for your humble hotel that consists of a plate being pushed up a shaft with a fountain of water where we can control the fountain strength, but you don’t really have any way to automate getting it to go to just the right height.

    You’ve tried writing down the proper fountain strengths for every floor, but changes in weight of the hamsters and lots of other factors make this unreliable. You’ve tried mathematically analyzing and accounting for all of these other factors, but not only is it impractical, but it can’t possibly account for dynamic changes in the system.

    Let’s take a look at a possible solution that might have some promise.

    Read more … Comments

  • The Hamster Hotel: An Introduction to Control Theory (Part 1)

    Over this summer my work has been in the field of control theory. I must admit that I at first found it a little odd that the idea of “controlling something” — say, the temperature of a room or the speed of a fan — could be an extremely deep/rich mathematical and engineering discipline.

    This series should hopefully be an enlightening walk through this wonderful world that is control theory, and explore (without any complex mathematics) just how much genius goes into those simple things we all take for granted.

    Welcome to the Hamster Hotel.

    Read more … Comments