Algorhythms
| (require algorhythms) | package: algorhythms |
A Racket library of algorithms and data structures. Every function documented below is exported by algorhythms; there are no submodule imports required for anything on this page.
1 Installation
Install from the Racket package catalog:
raco pkg install algorhythms |
Or from source:
git clone https://github.com/aryaghan-mutum/algorhythms.git |
cd algorhythms |
raco pkg install --link . |
2 Quick Start
(require algorhythms) (factorial 10) ; 3628800 (prime? 17) ; #t (encode-to-morse "SOS") ; "... --- ..." (add 1 2 3 4 5) ; 15 (variadic calculator) (quick-sort '(3 1 4 1 5) <) ; '(1 1 3 4 5)
3 Calculator (Arithmetic Operators)
Variadic calculator-style operators that accept any numeric type Racket supports (exact integers, exact rationals, inexact/decimals, complex). Under the hood, the binary implementations use pure recursion (add1 / sub1) for exact integers and delegate to the built-in numeric tower for non-integer inputs.
(multiply 2 3 4) ; 24
procedure
(modulus a b) → exact-integer?
a : exact-integer? b : (and/c exact-integer? (not/c zero?))
procedure
base : number? n : exact-nonnegative-integer?
procedure
(add-integers-recursive a b) → exact-integer?
a : exact-integer? b : exact-integer?
procedure
a : exact-integer? b : exact-integer?
procedure
(multiply-integers-loop a b) → exact-integer?
a : exact-integer? b : exact-integer?
4 Math
4.1 Combinatorics
procedure
n : exact-nonnegative-integer?
procedure
(unique-permutations lst) → (listof list?)
lst : list?
procedure
(pascal-triangle rows)
→ (listof (listof exact-positive-integer?)) rows : exact-positive-integer?
4.2 Number Theory
procedure
n : exact-integer?
procedure
n : exact-integer?
procedure
(primes-up-to-via-sieve n) → (listof exact-positive-integer?)
n : exact-integer?
procedure
n : exact-positive-integer?
procedure
(gcd-euclidean a b) → exact-integer?
a : exact-integer? b : exact-integer?
procedure
(lcm-custom a b) → exact-integer?
a : exact-integer? b : exact-integer?
procedure
n : exact-nonnegative-integer?
procedure
n : exact-positive-integer?
procedure
(leap-year? year) → boolean?
year : exact-integer?
procedure
(palindrome-num? x) → boolean?
x : exact-integer?
4.3 Arithmetic
procedure
n : exact-nonnegative-integer?
procedure
base : number? n : exact-nonnegative-integer?
4.4 Statistics
procedure
lst : (non-empty-listof real?)
procedure
lst : (non-empty-listof real?)
procedure
lst : (non-empty-listof any/c)
procedure
lst : (non-empty-listof real?)
procedure
(standard-deviation lst) → real?
lst : (non-empty-listof real?)
procedure
(percentile lst p) → real?
lst : (non-empty-listof real?) p : (real-in 0 100)
4.5 Financial
procedure
(simple-interest principal time rate) → real?
principal : real? time : real? rate : real?
procedure
(compound-interest principal time rate) → real?
principal : real? time : real? rate : real?
4.6 Matrix
procedure
(matrix-transpose m) → (listof list?)
m : (listof list?)
procedure
(matrix-determinant m) → real?
m : (listof list?)
procedure
(identity-matrix n) → (listof list?)
n : exact-positive-integer?
4.7 Algebra
procedure
(solve-linear a b) → real?
a : real? b : real?
procedure
base : number? n : exact-nonnegative-integer?
4.8 Trigonometry
procedure
(hypotenuse a b) → real?
a : real? b : real?
procedure
(degrees->radians deg) → real?
deg : real?
procedure
(radians->degrees rad) → real?
rad : real?
4.9 Geometry
procedure
(circle-area r) → real?
r : real?
procedure
(rectangle-area len wid) → real?
len : real? wid : real?
procedure
(area-of-triangle base height) → real?
base : real? height : real?
procedure
(sphere-volume r) → real?
r : real?
procedure
(cube-volume s) → real?
s : real?
procedure
(pythagoras x y) → real?
x : real? y : real?
4.10 Logarithms
5 Data Structures
5.1 Higher-Order Functions
procedure
fn : procedure? lst : list?
procedure
(filter-custom pred lst) → list?
pred : procedure? lst : list?
procedure
fn : procedure? lst : list?
procedure
(foldl-custom fn init lst) → any/c
fn : procedure? init : any/c lst : list?
procedure
(foldr-custom fn init lst) → any/c
fn : procedure? init : any/c lst : list?
procedure
(flatten-list lst) → list?
lst : any/c
procedure
(compose-fns fn ...) → procedure?
fn : procedure?
procedure
(pipe fn ...) → procedure?
fn : procedure?
procedure
(complement fn) → procedure?
fn : procedure?
procedure
5.2 Lists
procedure
(my-length lst) → exact-nonnegative-integer?
lst : list?
procedure
(penultimate lst) → any/c
lst : list?
procedure
(remove-last lst) → list?
lst : list?
procedure
lst : list? pos : exact-positive-integer?
procedure
(occurrences item lst) → exact-nonnegative-integer?
item : any/c lst : list?
procedure
(remove-element item lst) → list?
item : any/c lst : list?
procedure
(append-custom lst1 lst2) → list?
lst1 : list? lst2 : list?
procedure
n : exact-integer?
procedure
(alternative-elems lst) → list?
lst : list?
procedure
(member-custom? item lst) → boolean?
item : any/c lst : list?
procedure
(palindrome-lst? lst) → boolean?
lst : list?
5.3 Sets
procedure
(unique-elements lst) → list?
lst : list?
procedure
(set-intersection a b) → list?
a : list? b : list?
procedure
(duplicates-by-elem lst item) → list?
lst : list? item : any/c
procedure
(set-move-elem-to-last lst e) → list?
lst : (listof number?) e : number?
5.4 Sorting
procedure
(insertion-sort lst) → (listof real?)
lst : (listof real?)
procedure
(selection-sort lst) → (listof real?)
lst : (listof real?)
5.5 Strings
procedure
(reverse-words str) → string?
str : string?
procedure
(reverse-chars-in-str str) → string?
str : string?
procedure
(string-find pattern str) → (or/c exact-nonnegative-integer? #f)
pattern : string? str : string?
procedure
str : string?
procedure
(string-index-of-char ch str)
→ (or/c exact-nonnegative-integer? #f) ch : char? str : string?
procedure
(string-split-custom sep str) → (listof string?)
sep : char? str : string?
procedure
(string-join-custom sep lst) → string?
sep : char? lst : (listof string?)
procedure
(string-upcase-custom str) → string?
str : string?
procedure
(string-downcase-custom str) → string?
str : string?
5.6 Queue
procedure
(make-queue) → queue?
procedure
(queue-empty? q) → boolean?
q : queue?
procedure
(queue-peek q) → any/c
q : queue?
5.7 Stack
procedure
(make-stack) → procedure?
(define s (make-stack)) (s 'push! 1) (s 'push! 2) (s 'top) ; 2 (s 'pop!) (s 'top) ; 1
6 Encoding
procedure
(encode-to-morse str) → string?
str : string?
(encode-to-morse "SOS") ; "... --- ..." (encode-to-morse "HELLO") ; ".... . .-.. .-.. ---"
procedure
(decode-from-morse morse) → string?
morse : string?
7 License
BSD-3-Clause License. Copyright (c) 2024, Anurag Muthyam.