Elegant as fuck
1
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.
2
Name:
Anonymous
2011-05-15 20:45
one word: THE FORCED INDENTATION OF CODE
3
Name:
Anonymous
2011-05-15 21:18
a, b = b, a+b
Wait, how exactly does this resolve to the Fibonacci series?
4
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
5
Name:
Java Suit
2011-05-15 21:40
JAVA
7
Name:
Anonymous
2011-05-15 21:47
So it's functionally similar to a = b + (b = a)?
8
Name:
Anonymous
2011-05-15 22:08
>>4
fib:
:fibloop
There's something wrong with you're assembler.
9
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
10
Name:
Anonymous
2011-05-15 22:42
>>9
fib:
: fibloop
The're's something's wrong with you're assembler.
11
Name:
Anonymous
2011-05-16 1:06
U MENA fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
12
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
?
13
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.
14
Name:
Anonymous
2011-05-16 2:01
>>13
Please give me a status report on your anus.
15
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
16
Name:
Anonymous
2011-05-16 3:45
17
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;
18
Name:
Anonymous
2011-05-16 5:45
>>14
Welcome to DrJava interpreter console
anus.isEmpty()
true
19
Name:
Anonymous
2011-05-16 5:45
>>14
Welcome to DrJava interpreter console
> anus.isEmpty()
true
>
dicks
20
Name:
Anonymous
2011-05-16 7:49
>>17
U MENA PERL WITHOUT SIGILS
21
Name:
Anonymous
2011-05-16 11:07
22
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)
23
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.
24
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).
25
Name:
Anonymous
2011-05-16 16:53
>>23
Do any (popular) C compilers even optimize tail calls?
26
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
}
27
Name:
Anonymous
2011-05-16 17:35
>>25
Yes. I'm led to believe GCC does it at some optimisation level higher than 0.
28
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
29
Name:
Anonymous
2011-05-16 18:24
>>25
Only when the tailcalled procedure's signature is compatible with the caller's.
30
Name:
Anonymous
2011-05-16 18:51
proper anus recursion as fuck
31
Name:
Anonymous
2011-05-16 18:59
>>28
It seems expensive as fuck on x86.
32
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
33
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.
34
Name:
Anonymous
2011-05-16 21:03
fib = lambda n : return n + fib(n-1) if n and n - 1 else 1
35
Name:
Anonymous
2011-05-16 21:56
fib(10);
fib is in the stdlib. I win.
Newer Posts