Skip to content

Instantly share code, notes, and snippets.

View CarstenKoenig's full-sized avatar

Carsten König CarstenKoenig

View GitHub Profile
@CarstenKoenig
CarstenKoenig / free.fsx
Last active July 11, 2017 15:21
free monad example in F#
// translated from http://www.haskellforall.com/2012/06/you-could-have-invented-free-monads.html
// we need our functor that will define the DSL more or less
type Command<'next> =
| Output of string * 'next
| Bell of 'next
| Done
// we need the fmap for the functor
let fmap f = function
@CarstenKoenig
CarstenKoenig / ExistentialProjection.fsx
Last active July 10, 2017 19:24
existential projections in F#
// Projektion
type Key = int
type Projection<'ev,'st,'res> =
{
init : Key -> 'st
fold : 'st -> 'ev -> 'st
final : 'st -> 'res
}
type Movement =
| Left of int
| Right of int
type MovementState<'a> = Movement list -> Movement list * 'a
type MovementBuilder () =
member x.Zero () : MovementState<unit> = fun st -> (st, ())
member __.Return x : MovementState<'a> = fun st -> (st, x)
member __.Bind(m: MovementState<'a>, f) =
@CarstenKoenig
CarstenKoenig / AnotherRusMult.fs
Last active May 26, 2017 13:51
Russische Bauernmultiplikation / Ancient Egyptian Multiplication
// Algorithmus
// (DE) http://ccd-school.de/coding-dojo/function-katas/russische-bauernmultiplikation/
// (EN) https://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
let rec mul a b =
match a with
| 1 -> b
| a when a <= 0 -> 0
| a when x % 2 = 0 -> mul (a/2) (b*2)
| a -> mul (a/2) (b*2) + b
@CarstenKoenig
CarstenKoenig / Bauermult.hs
Last active May 25, 2017 12:42
Russische Bauernmultiplikation in Haskell
{----------------------------------------------------------------------
- CCD Function Kata
- Russische Bauernmultiplikation
-
- siehe: http://ccd-school.de/coding-dojo/function-katas/russische-bauernmultiplikation/
-
-
- Erklärung:
- ---------
- wie das funktioniert versteht am am einfachsten, wenn man sich die linke
@CarstenKoenig
CarstenKoenig / Bauermult.fsx
Last active May 25, 2017 12:41
Russische Bauernmultiplikation - F# Version
(***********************************************************************
* CCD Function Kata
* Russische Bauernmultiplikation
*
* siehe: http://ccd-school.de/coding-dojo/function-katas/russische-bauernmultiplikation/
*
*
* Erklärung:
* ---------
* wie das funktioniert versteht am am einfachsten, wenn man sich die linke
@CarstenKoenig
CarstenKoenig / FormatCombinations.fsx
Last active June 4, 2017 09:55
Recursive Problem / Data
(*****************************************************************************************
* Task:
* =====
*
* There is a string which may contain asterisks. Each asterisk must be replaced by
* a 0 or 1 and then all possible output combinations must be printed out.
*
* For example the strings
*
* a*b must produce two outputs a0b and a1b
@CarstenKoenig
CarstenKoenig / EightQ.hs
Created May 17, 2017 08:04
let's talk about abstract fuzzy things
module EightQ where
import Control.Monad (foldM)
type Queen = (Int,Int)
{-----------------------------------------------------------------------
Questions:
- would you consider `queens` readable?
- what about understanding?
@CarstenKoenig
CarstenKoenig / Matrizen.idr
Last active April 18, 2017 13:43
Matrixmult
module Matrizen
import Data.Vect
Matrix : (Nat, Nat) -> Type -> Type
Matrix (n,m) a = Vect n (Vect m a)
scalProd : Num a => Vect n a -> Vect n a -> a
scalProd xs ys = sum $ zipWith (*) xs ys
@CarstenKoenig
CarstenKoenig / MutableLinkesList.fs
Created March 6, 2017 05:55
reverse mutable linked list in F#
module List =
type Item = { value : int; next : List }
and List = Item option ref
let empty : List = ref None
let singleton x = { value = x; next = ref None }
let rec append x l =
let item = singleton x