Skip to content

Instantly share code, notes, and snippets.

View paniag's full-sized avatar

Eric Paniagua paniag

  • Sunnyvale, CA, USA
View GitHub Profile
#include <cstdlib>
#include <type_traits>
using std::enable_if;
using std::size_t;
////////////////////////////////////////////////////////////////////////////////
// Utility
////////////////////////////////////////////////////////////////////////////////
@paniag
paniag / list.hs
Last active July 5, 2016 23:40
Church Encodings
{-# LANGUAGE RankNTypes #-}
module List where
import Control.Applicative (Applicative, pure, (<*>), (<$>))
import Data.Foldable (Foldable, foldr)
import Data.Traversable (Traversable, traverse)
import Prelude hiding (foldr)
newtype ListC a =
ListC {
@paniag
paniag / lab3.hs
Last active October 14, 2018 20:51
Caltech CS 11 Haskell Lab 3
data AbstractInteger = Zero | Succ AbstractInteger | Pred AbstractInteger
deriving (Eq, Show)
instance Ord AbstractInteger where
-- compare :: AbstractInteger -> AbstractInteger -> Ordering
compare (Pred x) (Pred y) = compare x y
compare (Pred _) Zero = LT
compare (Pred _) (Succ _) = LT
compare Zero (Pred _) = GT
compare Zero Zero = EQ
@paniag
paniag / lab2.hs
Last active September 13, 2015 00:02
Caltech CS 11 Haskell Lab 2
--
-- lab2.hs
--
-- Sum a list of integers recursively.
sumIntegers1 :: [Integer] -> Integer
sumIntegers1 (x:xs) = x + sumIntegers1 xs
sumIntegers1 [] = 0
-- Sum a list of integers in terms of foldl.
@paniag
paniag / lab1.hs
Last active August 29, 2015 21:26
Caltech CS 11 Haskell Track Lab 1
add_and_double :: Double -> Double -> Double
add_and_double x y = (x + y) * 2
(+*) :: Double -> Double -> Double
(+*) x y = x `add_and_double` y
solve_quadratic_equation :: Double -> Double -> Double -> (Double, Double)
solve_quadratic_equation a b c = (x1, x2)
where disc = sqrt (b ** 2 - 4 * a * c)
x1 = (disc - b) / (2 * a)
@paniag
paniag / universal_sink.py
Last active September 23, 2015 18:28
Test whether a directed graph given as an adjacency matrix has a universal sink in O(V) time.
from collections import deque
def has_universal_sink (A):
V = len(A)
if not V:
return False
Q = deque(xrange(V))
while len(Q) > 1:
u = Q.popleft()
v = Q.popleft()