Skip to content

Instantly share code, notes, and snippets.

@aparatext
Last active November 19, 2024 22:00
Show Gist options
  • Select an option

  • Save aparatext/2ba2ad4be7051494e794b4e8d42abfda to your computer and use it in GitHub Desktop.

Select an option

Save aparatext/2ba2ad4be7051494e794b4e8d42abfda to your computer and use it in GitHub Desktop.
;; Triangle of Power operator in Scheme.
;; Tested in Racket, Guile, and Chicken; portable under R6RS+.
;; See https://mathcenter.oxford.emory.edu/site/math108/logs/
;; and https://www.youtube.com/watch?v=sULa9Lc4pck
;; and https://math.stackexchange.com/questions/30046/alternative-notation-for-exponents-logs-and-roots/165225#165225
;; to learn about "Triangle of Power" notation
;; Exponents Logs Radicals (triangle) operation
(define-syntax elr
;; Argument order is counterclockwise from bottom right.
(syntax-rules (?)
[(_ a b ?) (expt a b)]
[(_ a ? c) (/ (log c) (log a))]
[(_ ? b c) (expt c (/ 1 b))]
[(_ a b c) (= (expt a b) c)]))
;; ^ looks a bit like a △, but Racket already uses it and
;; it may look too similar to exponentiation
(define-syntax △
(syntax-rules ()
[(_ a b c) (elr a b c)]))
;; Examples:
(= (△ 3 #;raised-to 2 #;is ?) 9) ;; Exponentiation
(= (△ 3 ? 9) 2) ;; Logarithm
(= (△ ? 2 9) 3) ;; Root
(eq? (△ 3 2 9) #t) ;; Complete triangle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment