Skip to content

Instantly share code, notes, and snippets.

@ijt
ijt / io_quickcheck_example.hs
Created May 11, 2011 22:12
Simple example of monadic IO with QuickCheck in Haskell
#!/usr/bin/env runhaskell
-- Synopsis:
-- $ cabal install QuickCheck
-- $ runhaskell io_quickcheck_example.hs
--
-- Author: Issac Trotts <issac.trotts@gmail.com>
import Directory
import System.Environment
@ijt
ijt / applicative_quickcheck.hs
Created May 20, 2011 19:41
Example of checking a law for applicative functors using QuickCheck
#!/usr/bin/env runhaskell
import Control.Applicative
import Test.QuickCheck
newtype ZipList2 a = ZipList2 { getZipList :: [a] }
instance Applicative ZipList2 where
pure x = ZipList2 $ repeat x
(ZipList2 gs) <*> (ZipList2 xs) = ZipList2 (zipWith ($) gs xs)
@ijt
ijt / panic_example.go
Created May 24, 2011 07:02
Example of exception handling in Go
// Exception handling example, fleshed out from Rob Pike's post
// at http://goo.gl/ZiUra
package main
import "fmt"
func f(dopanic bool) {
defer func() {
if x := recover(); x != nil {
@ijt
ijt / ijt_hoogle.vim
Created June 5, 2011 20:51
Hoogle plugin for Haskell in Vim
" $ cabal install hoogle
"
" Then put this file in ~/.vim/ftplugin/haskell/ijt_hoogle.vim
"
" Put your cursor over a function name and press \h to find out
" its signature and where it is defined.
"
command! Hoogle :exec("!hoogle '" . expand("<cWORD>") . "'")
map \h :Hoogle<CR>
@ijt
ijt / demux.hs
Created June 28, 2011 00:36
Haskell program to demux input
#!/usr/bin/env runhaskell
import System.Environment
import System.Exit
import System.IO
usage = "usage: cat somefile | demux outfile1 outfile2 ..."
help = "This program alternates sending its input to outfile1 and outfile2."
main = do
@ijt
ijt / logging_example.hs
Created June 29, 2011 03:10
Example of logging in Haskell
#!/usr/bin/env runhaskell
-- This example uses the hslogger library.
-- For debugging it may be more convenient to use Debug.Trace instead since that
-- allows you to log debugging output from otherwise pure functions.
import System.IO (stderr, Handle)
import System.Log.Logger (rootLoggerName, setHandlers, updateGlobalLogger,
Priority(INFO), Priority(WARNING), infoM, debugM,
warningM, errorM, setLevel)
@ijt
ijt / tailf.hs
Created June 30, 2011 06:26
Haskell example of tailing a file
#!/usr/bin/env runhaskell
-- Example of tailing a file, using two communicating Haskell threads. It's not
-- really necessary to do this concurrently, but the approach here generalizes
-- nicely if we want to filter or transform the output.
import Control.Concurrent
import Control.Monad
import System.Environment
import System.Exit
@ijt
ijt / qc_test.go
Created August 2, 2011 09:03
Quickcheck of sorting idempotency in Go (golang)
package main
import (
"reflect"
"sort"
"testing"
"testing/quick"
)
type IntSlice []int
@ijt
ijt / random_monad_example.hs
Created October 3, 2011 00:13
Example of how to use the Random monad in Haskell
-- cabal install MonadRandom
-- ghc random_monad_example
-- ./random_monad_example
-- The code here is stolen from a comment in the MonadRandom source code.
import Control.Monad.Random
die :: RandomGen g => Rand g Int
die = getRandomR (1,6)
@ijt
ijt / median.hs
Created February 14, 2012 08:24
Print the median of a newline-separated list of numbers
#!/usr/bin/env runhaskell
import Data.List (sort, (\\))
import Test.QuickCheck (Property, (==>))
main = interact (showLn . median . map read . lines)
showLn :: Show a => a -> String
showLn x = show x ++ "\n"