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 / Pie.hs
Last active January 2, 2016 05:59
Building a pie chart using ThreePenny HTML canvas.
import Control.Monad
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
{-----------------------------------------------------------------------------
Main
------------------------------------------------------------------------------}
main :: IO ()
@kfl
kfl / Listfun.sml
Last active December 17, 2015 19:38
A few list functions written in different styles.
(* A few list functions written in different styles *)
fun len lst =
let fun loop acc [] = acc
| loop acc (_::xs) = loop (acc+1) xs
in loop 0 lst end
fun lenw lst =
let val cnt = ref 0
val next = ref lst
@kfl
kfl / Sideeffects.sml
Last active March 14, 2019 12:41
Fun with side-effects in SML
(* Moscow ML handle the first three tests correct. *)
fun csnd x y = y
local val r = ref "WRONG" in
val test1 = csnd (r := "OK"; ()) (!r);
end
local
val r = ref "WRONG"
@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
@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 / 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 / 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 / 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 / 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 / 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