Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am LambdaP on github.
  • I am lambdap (https://keybase.io/lambdap) on keybase.
  • I have a public key whose fingerprint is D0E6 C2AD D440 D0C3 151A 4116 E80A AB81 AA86 0901

To claim this, I am signing this object:

@LambdaP
LambdaP / quicksort.hs
Last active August 29, 2015 14:23
In-place quicksort in Haskell using STArrays.
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad.ST
import Data.Array.ST
quickSort :: (Ord a) => [a] -> [a]
quickSort l = runST $
do
arr <- newListArray (0, length l - 1) l
bounds <- getBounds arr
step bounds arr
@LambdaP
LambdaP / blackkeysieve.c
Created May 6, 2015 15:28
Black Key sieve algorithm implementation
/*
* Black Key sieve implementation
*
* This is a C99 implementation of the Black Key Sieve algorithm,
* as described by Larry Deering in [1]. It relies on the fact that all
* primes above 5 will be equal to {1, 7, 11, 13, 17, 19, 23, 29} modulo
* 30, allowing us to care only about the numbers satisfying this property,
* and to pack the information on 30 integers in a single byte.
*
* This code is released in the public domain.
@LambdaP
LambdaP / gist:edfe1b76d6028e85b63d
Last active August 29, 2015 14:17
Monad equivalence
{-# LANGUAGE UndecidableInstances, FlexibleInstances #-}
import Prelude hiding (return, join, (=<<))
-- A Monad can be defined either with (>>=) + return, or with
-- fmap + join + return
class Return r where
return :: a -> r a
@LambdaP
LambdaP / catamorphisms.lhs
Last active August 29, 2015 14:14
Organising my thoughts around catamorphisms.
> import Prelude hiding (pred)
We can write the Peano integers as a Haskell type:
>-- data Nat = Zero | Succ { pred :: Nat }
And alright, in ghci, we have what we expect
>-- λ :t Zero
>-- Zero :: Nat
@LambdaP
LambdaP / parse.hs
Created June 19, 2014 13:41
(Mostly) working parser for Linux syscall headers.
{-# LANGUAGE NoMonomorphismRestriction #-}
module Parse(parseFile
,Sys(..)
,Argument(..)
,Space(..)
) where
import Text.ParserCombinators.Parsec
import Data.List(intercalate)
#include <stdio.h>
int main (void)
{
const int c = 0; // NO MAGIC NUMMER GOOD PHRAKTICE
int *p = (int *) &c; // WAIT WAHT U DOIN
(*p)++; // NO STAHP
printf("%d\n", c); // 1 FFFFFFFFUUUUUUUUUUUUUUUUUUUUU
return 0; // FKUC DIS IM GOIN HOME
@LambdaP
LambdaP / better.c
Last active December 25, 2015 12:19
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WHILE(x) sizeof(x)
#define TRUE int
#define FALSE t
#define TIMES(x,y) (x+y)
#define PLUS(x) (x *)
#define EXIT(i) (i = 0)
#include <stdio.h>
int f(int, int);
int g(int);
int func(int, int);
int f(a,b)
{
int a, b;
@LambdaP
LambdaP / gist:5471027
Last active December 16, 2015 17:29
Basic implementation of an escaping mechanism.
#define ESCAPE_CHAR 242
#define START_CHAR 0x7E
#define END_CHAR 0x0D
void usb_putc(uint8_t c);
uint8_t usb_getc();
void usb_escaped_putc(uint8_t c)
{
switch(c) {