Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Knuth's up-arrow notation

Name: Anonymous 2007-08-07 11:37 ID:ywrNHFyl

(defun knuth (x n)
  (if (= n 2)
      (return-from knuth (expt x x))
    (return-from knuth (expt x (knuth x (- n 1))))
    )
)


Yes, an implementation in 6 lines of Lisp. What is Knuth's up-arrow notation? Wikipedia that shit. Don't try this with anything greater than (knuth 3 3), or you will overflow. (knut 3 3) == 3^3^3. (knuth 3 4) == 3^3^3^3.

Name: Anonymous 2007-08-08 23:48 ID:Heaven

>>45
\ escapes the ^ so it's pushed onto the stack instead of called.
also, 1- doesn't subtract from 1, it subtracts 1 from whatever number is on the top of the stack. so it's y-1, not 1-y.

>>44
: knuth ( x y -- z ) 2dup zero? [ 3drop 1 ] [ swap 1- knuth ^ ] if ;
or
: knuth ( x y -- z ) swap <array> 1 [ swap ^ ] reduce ;
is probably the most readable.
but i really like the fact that >>32 only uses one shuffle word.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List