Skip to content

Instantly share code, notes, and snippets.

@lborg019
lborg019 / dot-product.fsx
Created August 18, 2022 02:04
F# Dot Product
let rec inner xs ys =
match xs, ys with
| [], [] -> 0
| [], ys -> 0
| xs, [] -> 0
| x::xs, y::ys -> x * y + inner xs ys;;
(*
The head of the first list (xs) gets multiplied
by the head of the second list (ys).
We add the next call to this value and recursion
@lborg019
lborg019 / math-parser.fsx
Created August 18, 2022 02:17
fsharp monadic parser
type Exp =
| Num of int //number of integers
| Neg of Exp //negation
| Sum of Exp * Exp //sum
| Diff of Exp * Exp //difference
| Prod of Exp * Exp //product
| Quot of Exp * Exp;; //quotient
let rec evaluate = function
| Num n -> Some n
@lborg019
lborg019 / mergesort.fsx
Created August 18, 2022 02:40
fsharp merge sort
let rec split = function
| [] -> ([], [])
| [a] -> ([a], [])
| a::b::cs -> let (M,N) = split cs in (a::M, b::N)
let rec merge = function
| ([], ys) -> ys
| (xs, []) -> xs
| (x::xs, y::ys) -> if x < y then x :: merge (xs, y::ys)
else y :: merge (x::xs, ys)