Skip to content

Instantly share code, notes, and snippets.

@SuperSonicHub1
Created May 16, 2024 00:42
Show Gist options
  • Save SuperSonicHub1/1bdee17c09ec7ab606619a4c860b94f1 to your computer and use it in GitHub Desktop.
Save SuperSonicHub1/1bdee17c09ec7ab606619a4c860b94f1 to your computer and use it in GitHub Desktop.
My first Racket program: functional implementation of the hyperoperators
#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