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-07 12:26 ID:L1Wyxl+P

>>1
   )
>)

gtfo noob

Name: Anonymous 2007-08-07 12:42 ID:Heaven

I’M EXPERT PROGRAMMER
SON OF A BITCH LISP
LISP IS PIG
DO YOU WANT PARENTHESES?
DO YOU WANT PARENTHESESES?
LISP IS PIG DISGUSTING
KNUTH IS A MURDERER
FUCKING LISP)))))))))

Name: Anonymous 2007-08-07 12:45 ID:L1Wyxl+P

>>3
I’M EXPERT PROGRAMMER
SON OF A BITCH LISP
LISP IS PIG
DO YOU WANT PARENTHESES?
DO YOU WANT PARENTHESESES?
LISP IS PIG DISGUSTING
MCARTHY IS A MURDERER
FUCKING LISP)))))))))

Name: Anonymous 2007-08-07 13:07 ID:Heaven

>>4
I’M EXPERT MOOT
SON OF A BITCH ANONYMOUS
ANONYMOUS IS PIG
DO YOU WANT LULZ?
DO YOU WANT CP?
ANONYMOUS IS PIG DISGUSTING
SNACKS IS A MURDERER
FUCKING ANONYMOUS

Name: Anonymous 2007-08-07 14:05 ID:Heaven

Superior Lisp code:

((()())()()))()(()(((()))())((())(
 (()()()())
 (((())()
   (())
))

Name: Anonymous 2007-08-07 16:21 ID:Heaven


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


formatting fix'd

Name: WHAT WILL THIS CODE PRINT? 2007-08-07 16:22 ID:lt9gqYux


(define (foo a b)
  (let ((ex (print "hi")))
    (if a
        ex
        b)))
  
(foo #t (print "hrm"))

Name: Anonymous 2007-08-07 16:26 ID:L1Wyxl+P


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

idiotic redundancy reomved


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

fixed

Name: Anonymous 2007-08-07 17:29 ID:t01cDPRJ

>>1
You fucking suck.

knuth = (foldr1 (**) . ) . flip replicate

Name: Anonymous 2007-08-07 17:30 ID:8BUe1CYg

Really, can't lisp do better than that?

Naive Haskell:
knuth x 0 = 1
knuth x y = x ^ knuth x (y-1)

Name: Anonymous 2007-08-07 19:09 ID:Heaven

>>11
too late!

Name: Anonymous 2007-08-07 22:19 ID:eHiNpn4q

Even fagget liek me knows how to do n00b things like this with recursion in fucking every language.  Just like crappy C's version which is shorter
int knuth(int a, int b)
{
  if (b == 2) return a * a; else knuth(a, b - 1);
}

Name: Anonymous 2007-08-07 22:47 ID:yiVjEOLc

IS THIS A JOKE?  What happens when you call (knuth x 1)?  THINK ABOUT IT.

(defun knuth (x n)
  (if (= n 0)
      1
      (expt x (knuth x (- n 1)))))

Name: Anonymous 2007-08-07 22:49 ID:yiVjEOLc

OH GOD THE STUPID IS CONTAGIOUS

(defun knuth (x n)
  (if (= n 1)
      x
      (expt x (knuth x (- n 1)))))

Name: Anonymous 2007-08-07 22:50 ID:hfbhAME2

>>13
knuth(1, 1);

Name: Anonymous 2007-08-07 23:05 ID:yiVjEOLc

sigh...

(defun (knuth (x n)
  (if (= n -1)
    0
    (expt x (knuth x (- n 1)))))

so
(knuth 2 0) = 1
(knuth 2 1) = 2
etc...

Name: Anonymous 2007-08-07 23:11 ID:yiVjEOLc

Or you could just do in the easy way (Lua):

function knuth(x,n)
  local t = 0
  for (v=0,n) do t = x ^ t end
  return t
end

Name: Anonymous 2007-08-07 23:16 ID:UnRA5uA9

>>11
you called?
(defun (knuth x y)
  (if0 y 1 (expt (knuth x (-1 y)))))

Name: Anonymous 2007-08-07 23:18 ID:yiVjEOLc

THIS IS MY FINAL FORM

function knuth(x,n)
  math.exp(math.log(x) ^ n)
end

KEEPING IT REAL

Name: Anonymous 2007-08-07 23:27 ID:UnRA5uA9

or just

knuth x y = foldr1 (**) (replicate y x)
instead of the gayer, longer, less readable pointless version
knuth = (foldr1 (**) . ) . flip replicate

or even better
knuth x y = (iterate (**) x) !! y

Name: Anonymous 2007-08-08 0:10 ID:Heaven

>>20
Actually that doesn't work.  It would be cool if you could define it for fractions and negative numbers in a way that made sense though.

Name: Anonymous 2007-08-08 1:40 ID:REXV4D0K

HASKELL WINS

knuth x y = (iterate (**) x) !! y

Name: Anonymous 2007-08-08 4:33 ID:GDribYI2

>>23
You don't.

    Occurs check: cannot construct the infinite type: a = a -> a
    Probable cause: `**' is applied to too few arguments
    In the first argument of `iterate', namely `(**)'
    In the first argument of `(!!)', namely `(iterate (**) x)'

Name: Anonymous 2007-08-08 4:42 ID:Heaven

knuth _ 0 = 1
knuth x y = iterate (x^) x !! (y-1)

Name: Anonymous 2007-08-08 5:47 ID:REXV4D0K

>>24
HASKELL FAILS

LOL LOL HASKELL SUCKS LOL

Name: Anonymous 2007-08-08 8:24 ID:DDRzupTb

lisp fag version:
: knuth ( x y -- z ) 2dup zero? [ * ^ ] [ swap 1- knuth ^ ] if ;

haskell fag version:
: knuth ( x y -- z ) 2dup zero? [ * ^ ] [ swap 1- [ over swap ^ ] times nip ] if ;

better version:
: knuth ( x y -- z ) swap <array> 1 [ swap ^ ] reduce ;

Name: Anonymous 2007-08-08 8:24 ID:gBsPUUYP

Name: Anonymous 2007-08-08 8:30 ID:Heaven

>>28
Yes. An unreadable pointless function. Now what?

Name: Anonymous 2007-08-08 8:43 ID:Heaven

>>27
: knuth ( x y -- z ) 2dup zero? [ * ^ ] [ swap 1- knuth ^ ] if ;
is like
knuth _ 0 = 1
knuth x y = x ^ knuth x (y-1)


: knuth ( x y -- z ) 2dup zero? [ * ^ ] [ swap 1- [ over swap ^ ] times nip ] if ;
is like
knuth _ 0 = 1
knuth x y = iterate (x^) x !! (y-1)


: knuth ( x y -- z ) swap <array> 1 [ swap ^ ] reduce ;
is like
knuth = (foldr1 (**) . ) . flip replicate
or
knuth x y = foldr1 (**) (replicate y x)

>>29
in a practical language the pointless version is more readable than the other versions.

Name: Anonymous 2007-08-08 9:25 ID:Heaven

>>29
Which part, in particular, do you find unreadable? It looks very clear to me.

If point-free style is too much to handle for your pathetic little mind, of course you could do
knuth x n = foldr1 (**) $ replicate n x

Name: Anonymous 2007-08-08 11:23 ID:DDRzupTb

: knuth ( x y -- z ) [ swap <array> ] keep 1- \ ^ <array> append >quotation call ;

Name: Anonymous 2007-08-08 13:05 ID:mNCp9Qs0

These Factor snippets just keep convincing me that concatenative languages are full of fail. Can you obscure the intended meaning with stack shuffling any further?

Does anybody know of a concatenative language that isn't all about dup/swap/tuck/<stupid shuffleword here>? If it takes so much superfluous crap to gain any power, I'll achieve satori with a Lisp instead.

Name: Anonymous 2007-08-08 13:31 ID:E9iTUwZb

>>33
sure, 2 shuffle words is so much worse than 8 FUCKING PAIRS OF PARENTHESES.

Name: Anonymous 2007-08-08 13:39 ID:jQC43TuK

>>34
You're forgetting the $ operator there, dude. (lol.)

Name: Anonymous 2007-08-08 14:24 ID:mNCp9Qs0

>>35
Yes, because >>32 just leaps out and screams at me what its algorithm is. Oh, wait, it doesn't. If you claim it does, you're self-deluded.

I'm not partial to Lisp for other reasons, but even with all those parens it's far more readable. You don't need to do shit in reverse order and keeping a stack in your head.

And then there's the other functional languages, which are a huge improvement over Lisp readability-wise. Which leaves Factor permanently in the toy department. Forth was and is a dead end.

Name: Anonymous 2007-08-08 14:42 ID:oLee4JxW

SELECT FROM knuth LIKE x=y LEFT JOIN SELECT knuth LIKE y=y-1;

Eat that you scumbags. (Not functional code...)

Name: Anonymous 2007-08-08 15:37 ID:jfdJ4k27

>>36 I'm not partial to Lisp for other reasons, but even with all those parens it's far more readable.

Well, no kidding. Lisp is the most readable language around.

Name: Anonymous 2007-08-08 19:18 ID:Heaven

Best solution:
knuth x y = foldr (^) 1 $ replicate x y

Readable solution even retards get:
knuth x 0 = 1
knuth x y = x ^ knuth x (y-1)

LISP versions are only slightly worse
Factor SUCKS

THREAD OVER

Name: Anonymous 2007-08-08 20:56 ID:X4nQBBK3

>>24
sorry, I forgot an x, confused the ** operator with the ^ operator, and forgot !! counts from 0. I don't really program in haskell, I prefer lisp for my home stuff and I use pl/sql at work.
try
knuth x y = iterate (x^) x !! (y-1)

or if you want knuth x 0 = 1, then

knuth x y = (1 : iterate (x^) x ) !! y

iterate f x gives the infinite list, [x, f x, f (f x), ...]

since f here is (x^), we get

[x, x^x, x^(x^x), x^(x^(x^x)), ...]
we cons a 1
[1, x, x^x, x^(x^x), x^(x^(x^x)), ...]
then position 0 is knuth x 0, position 1 is knuth x 1, and so forth*.

* which incidentally, sucks big dick.



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