Skip to content

Instantly share code, notes, and snippets.

Avatar

Bill Motyl motylwg

View GitHub Profile
@motylwg
motylwg / hanoi.fs
Created Feb 17, 2015
Tower of Hanoi in F#
View hanoi.fs
type Peg =
| A
| B
| C
type Move =
| Move of int * Peg * Peg
static member toString m =
match m with
| Move(n, p1, p2) when n <> 1 -> sprintf "move %i %A %A" n p1 p2
View Sieve.fs
let rec sieve (xs : int seq) =
seq {
let p = xs |> Seq.take 1 |> Seq.exactlyOne
yield p
yield! xs |> Seq.skip 1 |> Seq.filter (fun x -> x % p <> 0) |> sieve
}
let ints = Seq.initInfinite (fun x -> x + 2)
let primes = sieve ints
@motylwg
motylwg / NQueens.fs
Last active Aug 29, 2015
N Queens in F#
View NQueens.fs
type position = int * int
type board = position list
let queens n =
let isNotAttack (p1 : position) (p2 : position) =
fst p1 <> fst p2 && snd p1 <> snd p2 && abs (fst p1 - fst p2) <> abs (snd p1 - snd p2)
let isSafe (b : board) =
match b with
| [] -> true
@motylwg
motylwg / primesStreamExample.scala
Created Jun 26, 2013
Sieve of Eratosthenes using Scala streams.
View primesStreamExample.scala
def sieve(s: Stream[Int]): Stream[Int] =
s.head #:: sieve(s.tail filter(_ % s.head != 0))
val primes = sieve(Stream.from(2))
primes.take(100).toList
You can’t perform that action at this time.