Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
class Dual f g | f -> g, g -> f where
zap :: (a -> b -> c) -> f a -> g b -> c
data PrintF a = PrintF String a deriving Functor
data CoPrintF a = CoPrintF { coPrint :: String -> a } deriving Functor
@pacak
pacak / fact2.hs
Last active August 29, 2015 14:21
hylomorphism factorial, no external dependencies
{-# LANGUAGE TemplateHaskell, DeriveFunctor #-}
fact1 :: Integer -> Integer
fact1 n = product [1..n]
reduceProduct [] = []
reduceProduct [x] = [x]
reduceProduct (a:b:xs) = (a*b) : reduceProduct xs
product' [] = 1
@pacak
pacak / fact.hs
Last active August 29, 2015 14:21
refold to compute factorial
{-# LANGUAGE TemplateHaskell, TypeFamilies, DeriveFunctor #-}
{-# LANGUAGE BangPatterns #-}
import Data.Functor.Foldable
import TH
import Data.List
fact1 :: Integer -> Integer
fact1 n = product [1..n]
@pacak
pacak / gist:ffcf9761ecfa55d642af
Last active August 29, 2015 14:21
1/0 knapsack using various stuff
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS -Wall #-}
module Main where
import Data.Functor.Foldable
import Control.Comonad.Cofree
type Item = (String, Int, Double)
@pacak
pacak / TH.hs
Last active August 29, 2015 14:21
Template haskell Foldable/Unfoldable instances and datatypes generation for recursion-schemes
{-# LANGUAGE LambdaCase, OverloadedStrings, FlexibleInstances #-}
{-# LANGUAGE ViewPatterns, TemplateHaskell #-}
module TH where
import Control.Applicative
import Control.Lens
import Control.Monad
import Data.Functor.Foldable
import Language.Haskell.TH
{-# OPTIONS -Wall #-}
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Conduit
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as C8L
import Data.List (intersperse)
@pacak
pacak / EteeArr.hs
Created July 5, 2012 05:02 — forked from JohnLato/EteeArr.hs
arrow-like functions for enumeratees
{-# LANGUAGE RankNTypes, ScopedTypeVariables, NoMonomorphismRestriction,
TupleSections #-}
-- | A couple arrow-like functions for enumeratees. Consider this example:
--
-- Suppose you have the following
--
-- 1. Consumer :: Iteratee [(Int,Char)] m a
-- 2. Mapper :: Enumeratee Int Char m a
-- 3. Source :: Enumerator Int m a