Skip to content

Instantly share code, notes, and snippets.

@gatlin
gatlin / list.lhs
Last active September 19, 2017 23:28
"Church-encoded" lists and their interesting properties
===
*This is a literate Haskell program which you can run in ghci.*
*Also, I already corrected a glaring problem. Let us speak no more of it.*
One of the beautiful things about computer science is that, fundamentally,
**data is code** and **code is data**. But what does that mean? And how is it
useful?
@gatlin
gatlin / bool.lhs
Last active August 29, 2015 14:06
What is a Church encoded data type?
===
I wrote a [post][listpost] about a really cool concept in computer science and
programming languages, but I fear that I may have gotten ahead of myself. It
turns out, Haskell is not so well known and notation was proving to be a
problem.
That and the concept is kind of a mind-bender.
@gatlin
gatlin / coro_switch.c
Last active August 29, 2015 14:07
Simple, toy coroutine mechanism in C
/*
* Simple coroutine mechanism in C
*
* Inspired by / stolen from:
*
* http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
*
* I found this very educational and wanted to publish a simple demonstration.
*
* Usage:

Recursion

For more information, see also [this enlightening post on recursion][fuckyou]

It's fucking weird. It's also the crucial property of a language that makes it Turing-complete (that is, powerful enough to compute anything which can be computed). [^1] It's often baked into a language and while it works it makes no sense intuitively.

@gatlin
gatlin / parser-combinator.rkt
Last active June 22, 2020 19:11
Simple parser combinator example in Typed Racket
#lang typed/racket
(provide None
Just
Opt
maybe
neq?
Parser
lit
try-parse
@gatlin
gatlin / qr.pl
Created December 18, 2014 20:36
I made a command line thing to let me quickly generate and display QR codes
#!/usr/bin/env perl
use v5.20;
use Gtk3 -init;
use Image::PNG::QRCode qw(qrpng);
use File::Temp qw(tempfile);
my $sample_data = $ARGV[0] // "gatlin is so handsome";
my $scale = 9;
@gatlin
gatlin / hellobike.hs
Last active August 29, 2015 14:11
Someone stole my bike! So I'm casting a wide net on Craigslist
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
import Prelude hiding (map)
import Pipes
import Pipes.Prelude (stdoutLn, map)
import Pipes.Interleave
import Network.Wreq
import qualified Data.ByteString.Lazy.Char8 as C
import qualified Data.ByteString.Char8 as SC
@gatlin
gatlin / Main.hs
Last active August 29, 2015 14:12
A (partial) re-implementation of the `memo` application available at http://getmemo.org. Excuse to learn about applicative parser combinators. Work in progress.
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Data.Char (toUpper)
import Prelude hiding (map, take, takeWhile, print, filter)
import Pipes
import Pipes.Prelude (map, fromHandle, toHandle, stdoutLn, filter, take, fold)
import Pipes.Lift
import System.Directory (getHomeDirectory, doesFileExist)
@gatlin
gatlin / withfeeling.sh
Last active December 12, 2015 19:00
Ever run a command in bash and forget you need to pull rank and use `sudo`? This alias allows you to issue the command again ... with feeling. I keep it in my `.bashrc` file.
withfeelingfn() {
PREV_WITH_SUDO=$(history | tail -n2 | head -n1 | tr -s ' ' | cut -d' ' -f3- | awk '{print "sudo " $0}')
command $PREV_WITH_SUDO
}
alias withfeeling=withfeelingfn
@gatlin
gatlin / applisp.hs
Created January 5, 2015 23:49
A simple parser combinator library and an even simpler lisp parser created with it.
{-# LANGUAGE DeriveFunctor #-}
import Control.Applicative hiding (many,optional)
import Data.Char
import Control.Monad.Free
newtype Parser s t = P {
runParser :: [s] -> [(t, [s])]
}