Created
May 16, 2024 00:42
-
-
Save SuperSonicHub1/1bdee17c09ec7ab606619a4c860b94f1 to your computer and use it in GitHub Desktop.
My first Racket program: functional implementation of the hyperoperators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require racket/match) | |
; https://en.wikipedia.org/wiki/Hyperoperation#Definition | |
; Because the base case is succession, it becomes unusable at tetration. | |
(define (hyperoperator-succ n a b) | |
(match (list n a b) | |
[(list 0 _ _) (+ b 1)] | |
[(list 1 _ 0) a] | |
[(list 2 _ 0) 0] | |
[(list n _ 0) #:when (>= n 3) 1] | |
[_ (hyperoperator (- n 1) a (hyperoperator n a (- b 1)))] | |
) | |
) | |
; Add more speedy base cases | |
(define (hyperoperator n a b) | |
(match (list n a b) | |
[(list 0 _ _) (+ b 1)] | |
[(list 1 _ _) (+ a b)] | |
[(list 2 _ _) (* a b)] | |
[(list 3 _ _) (expt a b)] | |
[(list n _ 0) #:when (> n 3) 1] | |
[_ (hyperoperator (- n 1) a (hyperoperator n a (- b 1)))] | |
) | |
) | |
(define (H n) (lambda (a b) (hyperoperator n a b))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment