Skip to content

Instantly share code, notes, and snippets.

@lamg
lamg / fibonacci.lean
Created November 4, 2026 11:38
Iterative and recursive Fibonacci have the same result
def fibonacci_rec
| 0 => 1
| 1 => 1
| Nat.succ n => fibonacci_rec n + fibonacci_rec (n - 1)
def fibonacci_iter (n: Nat) :=
let step := fun _ _ (fibₙ, «fibₙ₊₁») => («fibₙ₊₁», fibₙ + «fibₙ₊₁»)
(n.fold step (1,1))
#eval (List.range 7).map fibonacci_rec
@lamg
lamg / factorial_rec_iter.lean
Created November 4, 2026 11:30
Recursive and iterative factorial implementations are equivalent
def factorial_rec
| 0 => 1
| Nat.succ n => Nat.succ n * factorial_rec n
def factorial_iter (n: Nat) := (List.range' 1 n).prod
#eval factorial_rec 5
-- 120
#eval factorial_iter 5
-- 120
@lamg
lamg / config.lua
Created May 24, 2025 08:21
Lunar Vim F#
-- Read the docs: https://www.lunarvim.org/docs/configuration
vim.o.guifont = "Cascadia Code:h16"
vim.opt.shell = "/bin/fish"
-- indentation
vim.opt.tabstop = 2
vim.opt.shiftwidth = 2
vim.opt.smartindent = true
@lamg
lamg / z3_example.fsx
Created May 3, 2025 16:20
Using Z3 theorem prover in F#
#r "nuget: Microsoft.Z3.x64"
open Microsoft.Z3
let ctx = new Context()
let x = ctx.MkIntConst "x"
let y = ctx.MkIntConst "y"
// Define the premise: x > 0 && y > 0
let premise = ctx.MkAnd(ctx.MkGt(x, ctx.MkInt 0), ctx.MkGt(y, ctx.MkInt 0))
@lamg
lamg / print_tree.kts
Created May 1, 2025 10:57
print_tree.kts
sealed class Tree<out T> {
data class Leaf<T>(val value: T) : Tree<T>()
data class Branch<T>(
val value: T,
val children: List<Tree<T>>,
) : Tree<T>()
}
fun <T> printTree(tree: Tree<T>): List<String> {
fun connectIndent(isLast: Boolean, child: String, grandChildren: List<String>): List<String> {
@lamg
lamg / database_txn.fsx
Last active March 27, 2025 11:20
Computation Expression for handling transaction logic in SQLite
#r "nuget: Microsoft.Data.Sqlite"
#r "nuget: Oxpecker"
#r "nuget: FSharp.Control.TaskSeq"
open Microsoft.Data.Sqlite
open FSharp.Control
open System.Threading.Tasks
type User = { id: int64; name: string }
@lamg
lamg / decent_bfs_graphs.fsx
Created December 31, 2024 09:24
decent_bfs_graphs.fsx
// vertices are implicit in the set of values of type 'a
type Graph<'a when 'a: comparison> = { edges: Set<'a * 'a> }
let adjacent (g: Graph<'a>) (x: 'a) =
g.edges |> Set.filter (fun (y, _) -> x = y) |> Set.map snd
let graphLevels (startVertex: 'a) (g: Graph<'a>) =
let rec subLevels (level: Set<'a>, visited: Set<'a>) =
match level with
| _ when level.IsEmpty -> []
@lamg
lamg / decent_bfs.fsx
Created December 30, 2024 16:57
decent_bfs.fsx
type Tree<'a> = { value: 'a; children: Tree<'a> list }
let rec levels (ts: Tree<'a> list) =
match ts with
| [] -> []
| _ ->
let xs, chls = ts |> List.map (fun t -> t.value, t.children) |> List.unzip
let subs = chls |> List.concat |> levels
xs :: subs
@lamg
lamg / compare_side_by_side.fsx
Created November 22, 2024 17:27
Compare objects .Net
#r "nuget: ObjectDumper.NET, 4.3.4-pre"
let greenFragment x =
let ansiGreen = "\x1b[32m"
let ansiReset = "\x1b[0m"
$"%s{ansiGreen}%s{x}%s{ansiReset}"
let redFragment x =
let ansiRed = "\x1b[31m"
let ansiReset = "\x1b[0m"
@lamg
lamg / print_tree.fsx
Last active May 1, 2025 11:20
Print a tree
// the trick:
// each node must be connected directly to its direct children (with visible characters)
// and indirectly (through indentation) to its children's descendants
type Tree<'a> =
| Leaf of 'a
| Branch of 'a * list<Tree<'a>>
let printTree (t: Tree<'a>) =
let connectIndent (isLast: bool) (child: string, grandChild: string list) =