Skip to content

Instantly share code, notes, and snippets.

View kfl's full-sized avatar
🤓
Happy

Ken Friis Larsen kfl

🤓
Happy
View GitHub Profile
@kfl
kfl / memexercise.sml
Created June 11, 2010 07:23
Exercise the garbage collector
fun genVec n = Vector.tabulate(n, fn i => i)
local val gen = Random.newgenseed 42.0 in
fun random_length () = Random.range(500, 200*16192) gen
fun range rng = let val f = Random.range rng in fn () => f gen end
end
fun initial n = List.tabulate(n, fn _ => genVec(random_length()))
fun split_at i lst =
@kfl
kfl / lengthbench.hs
Created August 19, 2010 07:22
Benchmarking of build-in length and custom length function using criterion
{-
Benchmarking of build-in length and custom length function using criterion.
Compile with the command: ghc -O3 -W --make lengthbench.hs -o lengthbench
Results (timings in seconds, std. dev. negligible):
build-in length:
1000000 0.004
2000000 0.009
4000000 0.016
{-# LANGUAGE BangPatterns #-}
module Main (main) where
import System.Environment (getArgs)
import qualified Char
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
@kfl
kfl / gadtexpr.hs
Created January 10, 2012 14:14
Classic example on how to use GADTs
{-# LANGUAGE GADTs #-}
data Expr t where
Con :: Int -> Expr Int
Add :: Expr Int -> Expr Int -> Expr Int
Mult :: Expr Int -> Expr Int -> Expr Int
IsZ :: Expr Int -> Expr Bool
If :: Expr Bool -> Expr t -> Expr t -> Expr t
And :: Expr Bool -> Expr Bool -> Expr Bool
Or :: Expr Bool -> Expr Bool -> Expr Bool
@kfl
kfl / mapbench.hs
Created May 3, 2012 19:27
Benchmark template for TiPL 2012, for testing map implementations
-- Benchmark template for TiPL 2012, for testing map implementations
-- Compile with the command: ghc -O3 -W --make mapbench.hs -o mapbench
import qualified Criterion.Main as C
isPrime :: Integral a => a -> Bool
isPrime n = null [factor | factor <- [2..floor $ sqrt $ fromIntegral n]
, n `mod` factor == 0]
numbers = [2,3..]
@kfl
kfl / AccExp.hs
Created May 22, 2012 11:08
Generate a Array of powers with Accelerate
module AccExp where
import qualified Data.Array.Accelerate as A
import Data.Array.Accelerate (Z(..), (:.)(..),(!))
import qualified Data.Array.Accelerate.Interpreter as AI
pows :: A.Exp Double -> Int -> A.Acc(A.Vector Double)
pows u n =
A.generate (A.index1$ A.constant $ n+1) (\ix -> let i = A.unindex1 ix in u^i)
@kfl
kfl / gist:4064879
Created November 13, 2012 09:33
Capitalise text in Haskell
module Lorem where
import Control.Monad(replicateM_)
import System.Environment(getArgs)
import qualified System.IO as SIO
import qualified Data.Char as C
import qualified Data.ByteString.Char8 as BC
@kfl
kfl / Genrandom.hs
Created February 26, 2013 14:00
Benchmarking of simple random number generation using criterion.
{-
Benchmarking of simple random number generation using criterion.
Compile with the command: ghc -O3 -W --make -fforce-recomp -rtsopts Genrandom.hs -o Genrandom
To run the BOOM benchmark you need to set a bigger stack. For instance:
$ ./Genrandom BOOM +RTS -K256M -RTS
-}
@kfl
kfl / output from brew doctor
Created March 19, 2013 08:48
Failure to build John the Ripper under OS X 10.7.5
Warning: Setting DYLD_LIBRARY_PATH can break dynamic linking.
You should probably unset it.
Warning: /Library/Frameworks/Mono.framework detected
This can be picked up by CMake's build system and likely cause the build to
fail. You may need to move this file out of the way to compile CMake.
Warning: Unbrewed .pc files were found in /usr/local/lib/pkgconfig.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected .pc files:
@kfl
kfl / Dyn-ref.sml
Last active December 17, 2015 07:09
Dynamic types via exceptions or references in SML
structure Dyn :> Dyn =
struct
type t = unit -> unit
fun new () =
let val r = ref NONE
in { into = fn x => fn () => r := SOME x,
out = fn f => (f(); !r before r := NONE)}
end
end