Skip to content

Instantly share code, notes, and snippets.

@avalonalex
avalonalex / Cons.hs
Last active September 17, 2015 17:36
Cons.hs show function representation of pairs and positive integers in Haskell.
{-# LANGUAGE RankNTypes #-}
-- | Laws:
--
-- > car (ChurchTuple a b) == a
-- > cdr (ChurchTuple a b) == b
newtype ChurchTuple a b = ChurchTuple { unChurchTuple :: forall c. ((a -> b -> c) -> c) }
churchTuple :: a -> b -> ChurchTuple a b
churchTuple a b = ChurchTuple $ \m -> m a b
@avalonalex
avalonalex / .ghci
Created May 30, 2015 08:26
ghci config
:set prompt ">>> "
import Control.Applicative
import Control.Monad
import Data.ByteString (ByteString)
import Data.HashMap.Strict (HashMap)
import Data.Map (Map)
import Data.Set (Set)
import Data.Text (Text)
import qualified Data.ByteString as B
@avalonalex
avalonalex / gist:c5a378fa1af9820da57c
Created April 21, 2015 00:48
uninstall haskell platform
sudo rm -rf /Library/Frameworks/GHC.framework
sudo rm -rf /Library/Frameworks/HaskellPlatform.framework
sudo rm -rf /Library/Haskell
rm -rf .cabal
rm -rf .ghc
rm -rf ~/Library/Haskell
find /usr/bin /usr/local/bin -type l | \
xargs -If sh -c '/bin/echo -n f /; readlink f' | \
egrep '//Library/(Haskell|Frameworks/(GHC|HaskellPlatform).framework)' | \
cut -f 1 -d ' ' > /tmp/hs-bin-links
;; Keybonds
(global-set-key [(hyper a)] 'mark-whole-buffer)
(global-set-key [(hyper v)] 'yank)
(global-set-key [(hyper c)] 'kill-ring-save)
(global-set-key [(hyper s)] 'save-buffer)
(global-set-key [(hyper l)] 'goto-line)
(global-set-key [(hyper w)]
(lambda () (interactive) (delete-window)))
(global-set-key [(hyper z)] 'undo)
@avalonalex
avalonalex / ParserMonad.hs
Created June 30, 2014 00:39
a simple parser monad
{-# OPTIONS -Wall -fwarn-tabs -fno-warn-type-defaults #-}
-- The basic definition of the parsing monad.
module Parser (Parser,
get,
choose,
(<|>),
satisfy,
doParse,
# Thanks to this post:
# http://blog.ikato.com/post/15675823000/how-to-install-consolas-font-on-mac-os-x
$ brew install cabextract
$ cd ~/Downloads
$ mkdir consolas
$ cd consolas
$ curl -O http://download.microsoft.com/download/f/5/a/f5a3df76-d856-4a61-a6bd-722f52a5be26/PowerPointViewer.exe
$ cabextract PowerPointViewer.exe
$ cabextract ppviewer.cab
@avalonalex
avalonalex / Zip.hs
Last active March 6, 2018 13:14
An implementation of List Zip for moving `cursor` back and forth through list.
{-# OPTIONS -Wall -Werror -fwarn-tabs -fno-warn-type-defaults #-}
module Zip where
newtype Zip a = Z ([a], [a]) deriving Show
mkZip :: [a] -> Zip a
mkZip l = Z ([], l)
current :: Zip a -> a
@avalonalex
avalonalex / RSA.py
Last active March 6, 2021 10:46
A implementation of RSA public key encryption algorithms in python, this implementation is for educational purpose, and is not intended for real world use. Hope any one want to do computation like (a^b mode n) effectively find it useful.
#!/usr/bin/env python
import argparse
import copy
import math
import pickle
import random
from itertools import combinations