Skip to content

Instantly share code, notes, and snippets.

kill ((x:y:ys), zs) = kill (ys, zs ++ [x])
kill ([x], (_:zs)) = kill (zs, [x])
kill ([x], []) = x
kill ([], zs) = kill (zs, [])
run = kill ([1..100], [])
> run
73
{-# LANGUAGE TypeFamilies, UndecidableInstances #-}
data A = A
data B = B
data Found a = Found a
data NotFound = NotFound
type family F x
type instance F A = Found (Int -> Int)
type family FindF x where
FindF f = CheckFoundF (F f)
type family CheckFoundF res where
@deech
deech / Subtyping.hs
Last active September 22, 2015 06:23
Subtyping, OO-Style
{-# LANGUAGE GADTs, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances, FlexibleContexts, OverlappingInstances, ScopedTypeVariables #-}
-- The goal of the code below is to emulate OO method dispatch.
--
-- The use-case is binding to a C++ GUI framework that is heavily OO and
-- providing the user with a familiar experience.
--
-- This scheme sketched out below emulates not only OO style method dispatch
-- but also allows users to "sub-class", "override" and even arbitrarily
-- change the type signature of overridden methods, all without touching the
-- original library code.
@deech
deech / table-sort compile output -v3
Last active August 29, 2015 14:12
table-sort compile output -v3
[1 of 1] Compiling Main ( src/Examples/table-sort.hs, dist/build/table-sort/table-sort-tmp/Main.o )
*** Parser:
*** Renamer/typechecker:
*** Desugar:
Result size of Desugar (after optimization)
= {terms: 24,591, types: 551,63, coercions: 5,5}
src/Examples/table-sort.hs:89:17: Warning:
This binding for `isSelected' shadows the existing binding
imported from `Graphics.UI.FLTK.LowLevel.FLTKHS' at src/Examples/table-sort.hs:7:1-39
;; Neat way of getting all Emacs functionality in Evil's insert mode
(defun with-evil-mode (mode key-behavior-pairs)
(mapcar #'(lambda (pair)
(define-key
mode
(car pair)
(cdr pair)))
key-behavior-pairs))
@deech
deech / TypeLevelDebugger.hs
Created May 4, 2015 21:41
Type Level Debugger
{-# LANGUAGE GADTs, UndecidableInstances, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, FlexibleContexts, ScopedTypeVariables, OverlappingInstances, EmptyDataDecls #-}
data A a
data B a
data C a
data D a
data E a
data F a
type AToF =
@deech
deech / NonGreedy.hs
Created June 26, 2015 00:26
Non Greedy Parsing
import Control.Monad
import Text.Parsec
testString = unlines [
"foo {",
" contents of foo",
" a stray }",
"}",
"bar {",
"}"
@deech
deech / TypeFamily.hs
Created January 8, 2016 15:28
Searching a type level list using typeclasses vs. closed type families.
{-
This code shows how to check if a type-level list contains a given type.
It first shows the approach required for older versions of GHC (< 7.6.x)
and then a version using closed type families supported in GHC 7.8.1 and greater.
-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
@deech
deech / nset.el
Created March 5, 2016 00:27
Setting an element in a nested list structure
(defun nset-element-at (path ast new-element)
(if (= 0 (length path))
(setf ast new-element)
(let ((place-fn)
(path (reverse path)))
(progn
(dotimes (current-index (length path) nil)
(setq place-fn
(if (= current-index 0)
(list 'nth (nth current-index path) 'ast)
(define string-concat
{ (list string) --> string }
[] -> ""
[S] -> S
[S | Ss] -> (@s S " " (string-concat Ss)))
(datatype verified-types-for-lists
_______________________________________
(empty? Xs) : verified >> Xs : (list A);