Skip to content

Instantly share code, notes, and snippets.

@gatlin
gatlin / my-cons.scm
Last active December 30, 2015 10:59
DIY cons cell with car/cdr accessors using closures in Scheme (R5RS)
;; lovingly stolen from SICP
;; http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#%_sec_2.1.3
(define (my-cons x y)
(lambda (m) (m x y)))
(define (my-car z)
(z (lambda (p q) p)))
(define (my-cdr z)
@gatlin
gatlin / lc3.hs
Last active October 19, 2023 07:45
Unfinished proof of concept LC3 emulator in Haskell. My strategy is to build an EDSL which can be built up programmatically.
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Prelude hiding (not,and,log)
import Control.Monad
import Control.Monad.State
import Control.Monad.Free
import Control.Monad.Trans
import Control.Monad.Writer
import qualified Data.Vector.Unboxed as U
@gatlin
gatlin / nap.hs
Last active January 3, 2016 06:49
According to this Time article [1], people who sleep between 6.5 and 7.5 hours a night live the longest. The following program computes optimal amounts of time to sleep for both naps and nighttime. The variable circadian was chosen to be 21.83 because for n = 8 it gives almost exactly 24 hours and for n = 6 it gives 7 hours and 14 minutes. This …
{-
- Usage: ./nap [INT]
- [INT] is some positive integer (eg 1, 2, etc)
-}
import System.Environment (getArgs)
import Control.Applicative ((<$>), (<*>), pure)
circadian = 21.83
(define add (λ (n)
(λ (m)
(λ (f)
(λ (x)
((n f) ((m f) x)))))))
(define mult (λ (n)
(λ (m)
(λ (f)
(n (m f))))))
@gatlin
gatlin / adt.rkt
Created February 18, 2014 00:41
Playing with ADTs and other bullshit
#lang racket
; simple cons pairs
(define (my-cons fst snd)
(λ (f) (f fst snd)))
(define (my-first c)
(c (λ (a b) a)))
(define (my-second c)
@gatlin
gatlin / booldef.rkt
Last active August 29, 2015 13:57
Languages don't need built-in conditionals! Bah!
(define True (λ () (λ (a b) (a))))
(define False (λ () (λ (a b) (b))))
@gatlin
gatlin / imperative.lhs
Last active April 20, 2023 00:51
Imperative Programming in Haskell
How to add imperative programming to a pure functional language
===
Many people bemoan languages such as Haskell for not supporting imperative
programming; they decry the need for math in their computer science.
![Math? In my computer? Yeah right.](http://i.imgur.com/YDIaEPB.jpg)
I'm here to tell you that not only does Haskell make imperative programming a
cinch, but safe and correct as well. Follow along! This post is written in
@gatlin
gatlin / simple_alarm.hs
Created April 15, 2014 10:21
Load this into ghci, load a song into your MPD playlist, and then run startTimer with the correct number of seconds.
{-# LANGUAGE OverloadedStrings #-}
import Network.MPD
import Control.Concurrent.Timer
import Control.Concurrent.Suspend.Lifted
import System.Environment
startTimer seconds = oneShotTimer (withMPD (play Nothing) >> return ()) (sDelay (fromInteger seconds))
@gatlin
gatlin / lisp.py
Last active August 29, 2015 14:00
Tail recursive, algebraic definition of a simple cons list in Python, along with some example functions.
def tail_rec(fun):
'''
Receive a function `fun` as an argument;
Return a function which accepts `fun` and runs it in a loop.
'''
def tail(fun):
a = fun
while callable(a):
a = a()
return a
@gatlin
gatlin / fizzbuzz.hs
Last active August 29, 2015 14:01
Haskell FizzBuzz solution using monoids. I didn't write this but I wanted to separate it from its original article for didactic reasons.
{-# LANGUAGE MonadComprehensions #-}
-- From https://bmark.us/bmark/readable/060063d3dd0330
import Data.Monoid ((<>))
import Data.Maybe (fromMaybe)
import Control.Monad (mapM_)
fizzbuzz x = fromMaybe (show x) $ [ "fizz" | x `rem` 3 == 0 ]
<> [ "buzz" | x `rem` 5 == 0 ]