Skip to content

Instantly share code, notes, and snippets.

View CarstenKoenig's full-sized avatar

Carsten König CarstenKoenig

View GitHub Profile
@CarstenKoenig
CarstenKoenig / MontyHall.fs
Created February 22, 2018 13:19
Monty-Hall with Probability-Monad in F#
type Probability = double
type Distribution<'a> = ('a * Probability) list
type Event<'a> = 'a -> bool
let printDistribution (v : Distribution<'a>) =
let negate x = -x
v |> List.sortBy (snd >> negate) |> List.iter (fun (a,p) -> printfn "%A: %.2f%%" a (p * 100.0))
let sure (a : 'a) : Distribution<'a> =
[a, 1.0]
@CarstenKoenig
CarstenKoenig / Hylo.fs
Last active February 19, 2018 07:08
Factorial using a Hylomorphism in F#
type List<'i,'r> = Nil | Cons of 'i*'r
type FixList<'i> = FixList of List<'i,FixList<'i>>
let rec fmap (f : 'a -> 'b) (l : List<'i,'a>) : List<'i,'b> =
match l with
| Nil -> Nil
| Cons (x, tail) -> Cons (x, f tail)
// you can express hylo directly without using ana and cata (by either following the
@CarstenKoenig
CarstenKoenig / DepSum.idr
Last active December 29, 2018 09:47
dep. arity sum function in idris
module SimpleDep
basic : (b : Bool) -> if b then String else Int
basic True = "it's true"
basic False = 0
-------------
data WhatIsIt = AString | AInt
@CarstenKoenig
CarstenKoenig / SimpleComplicatedDT.hs
Created December 5, 2017 17:24
"dependent" example in Haskell
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
module DepFun where
data Nat = NatZ | NatS Nat
type family Codom (n :: Nat) where
@CarstenKoenig
CarstenKoenig / ExProj.fsx
Created October 18, 2017 19:59
existential projections in F#
// Projektion
type Key = int
type Projection<'ev,'st,'res> =
{
init : Key -> 'st
fold : 'st -> 'ev -> 'st
final : 'st -> 'res
}
@CarstenKoenig
CarstenKoenig / FinVecs.hs
Created October 12, 2017 20:39
some type-level stuff
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
module FinVec where
-- simple representation of naturals
-- DataKinds lifts this to the type-level
@CarstenKoenig
CarstenKoenig / Monaden.hs
Created October 12, 2017 10:02
MonadenAufgabe
{-# LANGUAGE InstanceSigs #-}
module ReaderM where
import Control.Monad.Reader
import Control.Monad.State.Strict
import Data.Functor.Identity
data Config = Config { nr :: Int }
main :: IO ()
@CarstenKoenig
CarstenKoenig / ScottyScriptDemo.hs
Created September 25, 2017 15:42
Demonstration of getting a scotty server running as a script - needs stack
#!/usr/bin/env stack
-- stack --resolver lts-9.5 script --package scotty
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
main = scotty 3000 $ do
@CarstenKoenig
CarstenKoenig / SigInt.hs
Created August 11, 2017 20:34
handling sigINT and sigTERM in Haskell (posix)
module Main where
import Control.Concurrent (threadDelay, MVar, newEmptyMVar, putMVar, tryTakeMVar)
import Data.Maybe (isJust)
import System.IO (hSetBuffering, BufferMode(NoBuffering), stdout)
import System.Posix.Signals (installHandler, Handler(Catch), sigINT, sigTERM)
@CarstenKoenig
CarstenKoenig / StackQuickStart.md
Created August 8, 2017 15:59
stack quick-example

1. Getting Stack on Ubuntu

I would actually use the installer script:

sudo curl -sSL https://get.haskellstack.org/ | sh

but an older version is on official sources as well (better to the upgrade after)

sudo apt install haskell-stack
stack upgrade