Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar
🐧
Arch btw

Arnau Abella monadplus

🐧
Arch btw
View GitHub Profile
@monadplus
monadplus / ListT.hs
Created April 29, 2020 08:54
ListT done it right!
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
-- Source: https://wiki.haskell.org/ListT_done_right
-- The reason why https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-List.html is broken:
-- * ListT imposes unnecessary strictness.
-- * ListT isn't really a monad transformer, ie. ListT m isn't always a monad for a monad m.
data Tree' a = Leaf' | Node' a (Tree a) (Tree a)
data Tree a = Tree { root :: Tree' a, force :: () }

instance NFData a => NFData (Tree a) where
  rnf = force

node :: NFData a => a -> Tree a -> Tree a -> Tree a
node x l r = Tree (Node' x l r) (rnf x `seq` rnf l `seq` rnf r)
@monadplus
monadplus / random_access_lists.md
Created May 4, 2020 12:45
Random Access Lists
@monadplus
monadplus / Main.hs
Created May 7, 2020 14:30
Generate ADN chain of 10GB.
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Test.QuickCheck
import System.IO
import Data.List.Split
import Text.Printf
data DNA
@monadplus
monadplus / expression_problem.md
Created May 8, 2020 12:30
The Expression Problem

The Expression Problem basically states that programs are made up of data types and functions that act on those data types. As a program grows, the number of functions that rely on the structure of those data types also grows. Because of this, extending the program or adding more functionality becomes exponentially more difficult.

@monadplus
monadplus / a_star_algo.md
Last active May 20, 2020 11:36
A* algorithm

A*

Compute the shortest path between two vertices of a graph.

  • At each step picks the node according to the lowest f(x) = g(x) + h(x)
  • g(x) = the movement cost to move from the starting point to a given square
  • h(x) = estimated movement cost from that given square to the final destination. Heuristic

Time Complexity

@monadplus
monadplus / binomial-heaps.md
Last active May 25, 2020 15:39
Binomial Heaps: distilled

Binomial Heaps [Vui78, Bro78]

Common implementation of heaps. [Easy to implement as a purely functional data structure][1].

Complexity

Operation Binary Binomial Fibonacci
Find-min Θ(1) Θ(1) Θ(1)
delete-min Θ(log n) Θ(log n) O(log n)
@monadplus
monadplus / command.md
Created June 23, 2020 18:40
LLVM backend for GHC
# Requires LLVM 6.0 installed
ghc -fllvm -optlo-O3 bubblesort.hs
@monadplus
monadplus / DeriveSetter.hs
Last active July 1, 2020 09:47
Derive Setter by Samuli Thomasson
{-# LANGUAGE TemplateHaskell #-}
module TH (
deriveSetters
) where
import Control.Monad (forM)
import Data.List (nub)
import Language.Haskell.TH
-- From field name to update function
@monadplus
monadplus / CTE.sql
Created September 17, 2020 17:29
Postgresql: Common Term Expressions (CTE)
-- Common Table Expressions
-- : Problem
-- 1) For each film count the number of actors starring in it
SELECT f.id, count(*) as actors FROM films f LEFT JOIN films_actors fa ON f.id = fa.film_id GROUP BY f.id;
-- 2) For each film, #actors, its prequel and sequel count number of actors starring in them.
SELECT films.id, films.prequel_id, films.sequel_id,
films.actors AS actors,