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

Elegant as fuck

Name: Anonymous 2011-05-15 20:42

def fib(times):
    a, b = 0,1
    for _ in range(times):
        a, b = b, a+b
    return a


I think I just creamed my pants.

Name: Anonymous 2011-05-15 20:45

one word: THE FORCED INDENTATION OF CODE

Name: Anonymous 2011-05-15 21:18

a, b = b, a+b
Wait, how exactly does this resolve to the Fibonacci series?

Name: Anonymous 2011-05-15 21:29

>>3
fib: ; calculates (ECX-1)th fibonacci number in EAX
 mov eax 1 | cdq
 :fibloop
 add eax edx | xchg eax edx
 loop :fibloop
 ret

Name: Java Suit 2011-05-15 21:40

JAVA

Name: Anonymous 2011-05-15 21:47

So it's functionally similar to a = b + (b = a)?

Name: Anonymous 2011-05-15 22:08

>>4
fib:
:fibloop

There's something wrong with you're assembler.

Name: Anonymous 2011-05-15 22:36

>>8
fib: ; calculates (ECX-1)th fibonacci number in EAX
mov eax 1 | cdq
:fibloop
add eax edx | xchg eax edx
loop:fibloop
ret

Name: Anonymous 2011-05-15 22:42

>>9
fib:
:fibloop

The're's something's wrong with you're assembler.

Name: Anonymous 2011-05-16 1:06

U MENA fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

Name: Anonymous 2011-05-16 1:19

you mena


def fib n
  a, b = 0, 1
  n.times {a, b = b, a + b}
  return a
end

?

Name: Anonymous 2011-05-16 1:36

How's this?

int fib (int a, int b, int n)
{
    if (n) return fib (b, a+b, n-1);
    else return a;
}

and you call it like this:

fib (1, 1, times)

All in glorious C.

Name: Anonymous 2011-05-16 2:01

>>13
Please give me a status report on your anus.

Name: Anonymous 2011-05-16 2:20


(define (fib n)
  (fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
  (cond ((= count 0) b)
        ((even? count)
         (fib-iter a
                   b
                   (+ (* p p) (* q q))
                   (+ (* q q) (* 2 q p))
                   (/ count 2)))
        (else (fib-iter (+ (* b q) (* a q) (* a p))
                        (+ (* b p) (* a q))
                        p
                        q
                        (- count 1)))))

SICP QUALITY

Name: Anonymous 2011-05-16 3:45

>>13
fib(100);

Name: Anonymous 2011-05-16 5:32

check out my Algol DSL

var fibs = -> n (
    var rec;
    rec = -> a b i (
        if !i then
            a;
        else
            rec(b,a+b,i-1);
        end;
    );
    rec 0 1 n;
);

print fibs 10;

Name: Anonymous 2011-05-16 5:45

>>14
Welcome to DrJava interpreter console
anus.isEmpty()
true

Name: Anonymous 2011-05-16 5:45

>>14
Welcome to DrJava interpreter console
> anus.isEmpty()
true
>

dicks

Name: Anonymous 2011-05-16 7:49

>>17
U MENA PERL WITHOUT SIGILS

Name: Anonymous 2011-05-16 11:07

Name: Anonymous 2011-05-16 14:57

>>13
fib(5, 5, 8);
OH WHOOPS LOOKS LIKE THATS NOT FIBS NOW IS IT?


def fib(n, a=0, b=1):
  if n:
    return fib(n-1, b, a+b)
  else:
    return a



print fib(100)

Name: Anonymous 2011-05-16 15:25

>>21
ARE YOU FUCKING RETARDED >>13 IS JUST A TAIL-RECURSIVE SOLUTION WITHOUT A SUGARY WRAPPER FUNCTION AND IT SAYS IT RIGHT THERE IN >>13 HOW YOU CALL IT.

Name: Anonymous 2011-05-16 16:25

>>23
AND IT SAYS IT RIGHT THERE IN >>13 HOW YOU CALL IT.
It should be fib(0, 1, times).

Name: Anonymous 2011-05-16 16:53

>>23
Do any (popular) C compilers even optimize tail calls?

Name: Anonymous 2011-05-16 17:19

>>1
sub fibs()
{
  my ($a, $b) = (0, 1);
  for (0 .. shift) {
    ($a, $b) = ($b, $a+$b);
  }
  return $a
}

Name: Anonymous 2011-05-16 17:35

>>25
Yes. I'm led to believe GCC does it at some optimisation level higher than 0.

Name: Anonymous 2011-05-16 17:49

Have you read your ``Proper Tail Recursion in C'' today?

http://www.complang.tuwien.ac.at/schani/diplarb.ps

Name: Anonymous 2011-05-16 18:24

>>25
Only when the tailcalled procedure's signature is compatible with the caller's.

Name: Anonymous 2011-05-16 18:51

proper anus recursion as fuck

Name: Anonymous 2011-05-16 18:59

>>28
It seems expensive as fuck on x86.

Name: Anonymous 2011-05-16 19:20

>>31
Uh? It's just turning a tail call into a goto. Though I haven't read that thing in >>28

Name: Anonymous 2011-05-16 20:01

>>32
You also have to rearrange the stack. On x86, the return address is stored on the stack, so it's one more thing to worry about while rearranging.
No problem if you're using a calling convention that uses registers for the return value and, possibly, the arguments.

Name: Anonymous 2011-05-16 21:03

fib = lambda n : return n + fib(n-1) if n and n - 1 else 1

Name: Anonymous 2011-05-16 21:56

fib(10);
fib is in the stdlib. I win.

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