Skip to content

Instantly share code, notes, and snippets.

View motylwg's full-sized avatar

Bill Motyl motylwg

  • SUNY Cobleskill
View GitHub Profile
@motylwg
motylwg / hanoi.fs
Created February 17, 2015 16:56
Tower of Hanoi in F#
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
@motylwg
motylwg / Sieve.fs
Last active August 29, 2015 14:15
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 August 29, 2015 14:04
N Queens in F#
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 June 26, 2013 15:42
Sieve of Eratosthenes using Scala streams.
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