Skip to content

Instantly share code, notes, and snippets.

@merijn
merijn / examples.hs
Created May 3, 2015 09:15
Small examples
let myMap = fix (\map' f list -> case list of [] -> []; (x:xs) -> f x : map' f xs) in myMap (+1) [1..]
@merijn
merijn / DropAt.hs
Created June 12, 2015 19:41
dropAt
dropAt :: Int -> [a] -> [a]
dropAt = go id
where
go :: ([b] -> [b]) -> Int -> [b] -> [b]
go f n [] = f []
go f 0 (x:xs) = f xs
go f n (x:xs) = go (f . (x:)) (n-1) xs
@merijn
merijn / Split.hs
Last active January 18, 2016 14:43
Efficient split in half
splitInHalf :: [a] -> ([a], [a])
splitInHalf [] = ([], [])
splitInHalf xs = go id xs xs
where
go :: ([b] -> [b]) -> [b] -> [b] -> ([b], [b])
go f xs [] = (f [], xs)
go f xs [_] = (f [], xs)
go f (x:xs) (_:_:ys) = go (f . (x:)) xs ys
@merijn
merijn / Scraper.hs
Created July 9, 2015 08:20
Concurrent webscraper
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent.Async
import Control.Concurrent.QSem
import Control.Monad
import Control.Monad.Catch
import Control.Monad.Trans
import Control.Monad.Reader
import Data.ByteString.Lazy (ByteString)
Authors

Merijn Verstraaten

Date

2014/11/22

So You Want to Be a Super Cool GHC Hacker?

So you have a pet peeve/bug/feature request that you'd like to see added to GHC. You made sure there was a Trac ticket for it, but despite your patient waiting no one is solving your problem. Those selfish GHC hackers!

@merijn
merijn / Foo.hs
Created August 23, 2014 07:01
Minimal C main FFI example
{-# LANGUAGE ForeignFunctionInterface #-}
module Foo where
import Foreign.C.String
import Foreign.C.Types
foreign export ccall start_server :: CInt -> CString -> IO ()
foreign export ccall start_client :: CString -> IO ()
start_server :: CInt -> CString -> IO ()
@merijn
merijn / NoRankN.hs
Last active December 22, 2016 08:21
Haskell RankNType example
module RankN where
data Foo = Foo Int | Bar Double
mangle :: Num a => (a -> a) -> Foo -> Foo
mangle f (Foo i) = Foo (f i)
mangle f (Bar d) = Bar (f d)
{-
NoRankN.hs:6:25:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module HList where
import Prelude hiding (head, tail, zip)
@merijn
merijn / gist:39dc86e345e87276c523
Last active January 2, 2019 10:07
Index list in haskell
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
import GHC.Prim (Constraint)
data Nat = Succ Nat | Zero
@merijn
merijn / gist:6130082
Last active January 2, 2019 10:07
Type family that disallows certain types for type variables.
{-# LANGUAGE ConstraintKinds, DataKinds, PolyKinds, TypeFamilies, TypeOperators #-}
import GHC.Exts (Constraint)
type family Restrict (a :: k) (as :: [k]) :: Constraint where
Restrict a (a ': as) = ("Error!" ~ "Tried to apply a restricted type!")
Restrict x (a ': as) = Restrict x as
Restrict x '[] = ()
foo :: Restrict a [(), Int] => a -> a