Saturday, May 7, 2011

Hyper-operations

Hyper-operations are an infinite sequence of arithmetic operations, which may be implemented on lists of unsigned integers with the following algorithm:

(defmulti hyper-operation
  (fn [n & nums] (zero? n)))

(defmethod hyper-operation true
  [n & nums] (+ (first nums) (count nums)))

(defmethod hyper-operation false
  ([n] (if (<= n 1) 0 1))
  ([n a] a)
  ([n a b]
    (if (and (= n 1) (zero? b)) 
      a
      (apply hyper-operation (dec n) (repeat b a))))
  ([n a b & args]
    (reduce (partial hyper-operation n) (hyper-operation n a b) args)))
The first five hyper-operations have names of their own, which can be specified by calling the partial function:
(def zeration (partial hyper-operation 0))
(def addition (partial hyper-operation 1))
(def multiplication (partial hyper-operation 2))
(def exponentation (partial hyper-operation 3))
(def tetration (partial hyper-operation 4))

No comments:

Post a Comment