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

Fibonacci

Name: Anonymous 2007-07-24 19:07 ID:DzKI9Xsx


fibs :: [Int]
fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)]

Fibonacci

Name: Anonymous 2007-07-24 19:20 ID:Heaven

zipWith

Name: Anonymous 2007-07-24 19:24 ID:B5oxyorC

fun fac 0 = 1
  | fac n = n * fac(n-1)

Name: Anonymous 2007-07-24 19:37 ID:B5oxyorC

>>2
winzip?

Name: Anonymous 2007-07-24 23:32 ID:yKjQ8H1t

fibs = 0 : 1 : zipWith (+) fibs (tail fibs) ???

btw zipWith is a weird name for what's esentially a map. Yes, I know it zips, uncurries, and maps, but well... it's a map, at least to my lisp mind

Name: Anonymous 2007-07-24 23:45 ID:D96w+bzt

fib[0]:0; fib[1]:1; fib[n]:=fib[n-1]+fib[n-2];

pwnd by maxima

Name: Anonymous 2007-07-25 0:11 ID:UXw5pLe1

I have written one in my time for my home page, fully optimized, unlike yours:


<?php
function calculateFibonacciNumber( $Number ) {
    if($Number == 1) {
        return $Number;
    }
#    else {
        var $Result;
#        $Result = calculateFibonacciNumber($Number-1)*calculateFibonacciNumber($Number-2);
#        return ($Result-1)+($Result-2);
#        $Result = calculateFibonacciNumber($Number-1)+calculateFibonacciNumber($Number-2);
#    }

    if($Number == 0) { # Thanks, Alex
        return $Number;
    }
   
    $Result=calculateFibonacciNumber($Number-1);
   
    if($Number == 2) {
        return 1;
    }
    if($Number == 3) {
        return 2;
    }
    if($Number == 3) {
        return 2;
    }
   
    $Result=$Result+calculateFibonacciNumber($Number-2);
   
    return $Result;
}
>

Name: Anonymous 2007-07-25 0:18 ID:2wPabZzP

>>7

I fucking lol'd hard.

Name: Anonymous 2007-07-25 3:28 ID:vPXK5law

>>7
enterprise crap? in MY PHP?

Name: Anonymous 2007-07-25 6:37 ID:JA2jnrZY

>>7
You fool! You forgot validating $Number now ur site is vulnerable to XSS attacks u gonna get hacked. Heres some fix for it.

function calculateFibonacciNumber( $Number = "" ) {
    //modified by Anonymous
    if(!empty($Number)){
        //do nothing
    }else{
        echo "DEBUG: \"\$Number\" is empty";
        return true; //true if empty, else Fibonacci
    }
    if(!((trim("".$Number.""))==($Number))){
        $Number = $Number + 0; //web code snippet from www.snippetsrus.com
    }
    $Number = (int) $Number;


The rest is the same.

Name: Anonymous 2007-07-25 7:58 ID:bkaOUevF

You guys know that Fib can be calculated in O(C)?

Name: Anonymous 2007-07-25 8:25 ID:JA2jnrZY

>>11
But mine is faster I'm using echo instead of printf

Name: Anonymous 2007-07-25 8:26 ID:s6+qCWd7

>>11

Yes, using Binet's formula. Or a lookup table.

Name: Anonymous 2007-07-25 10:33 ID:Heaven

: fib ( n -- n ) 5 sqrt tuck dup 1 swap - swap 1+ pick ^ swap pick ^ - -rot 2^ * / ;

Name: Anonymous 2007-07-25 13:02 ID:moFgDKky

>>14
One-liners don't go well with Forth^WFactor.

If you want to play unreadable golf, use Perl.

Name: Anonymous 2007-07-25 13:32 ID:6DvJsyV2

swap swap swap

Name: Anonymous 2007-07-25 13:54 ID:moFgDKky

Apparently this might get added to Factor 0.90: http://useless-factor.blogspot.com/2007/07/thats-dirtiest-macro-ive-ever-seen.html

So something like "tuck dup" can be rewritten as "xy-yxx". It's a lot easier to track what you're doing when you have long chains of stack manipulation.

Name: Anonymous 2007-07-25 13:59 ID:moFgDKky

"xy-yxyy"
fix'd

Goes to show that stack shuffling sucks.

Name: Anonymous 2007-07-25 14:10 ID:cSm+xp4k

>>17
reminds me of car/cdr/caar/cadr/cddr and some of the more exotic lisp macros

Name: Anonymous 2007-07-25 14:11 ID:cSm+xp4k

thread dup sage

Name: Anonymous 2007-07-25 17:03 ID:U1SdcRZy

Factor is halfway between Lisp and Brainfuck

Name: Anonymous 2007-07-25 17:12 ID:eFpIfUaa

>>15
perl?
sub fib{my$n=pop,$f=5**.5;return+((1+$f)**$n-(1-$f)**$n)/($f*2**$n)}
: fib ( n -- n ) 1 5 sqrt 3dup 3dup + swap ^ -roll - swap ^ -roll rot 2^ * * /;
look at that, it's only 10 characters shorter than it is in factor!

Name: Anonymous 2007-07-25 17:14 ID:Heaven

>>22
oops.
s/10/11/

Name: Anonymous 2007-07-25 17:21 ID:Heaven

>>22,23
Potentially same people.

Name: Anonymous 2007-07-25 19:14 ID:moFgDKky

>>22
The Perl version is more readable too. D:

Name: Anonymous 2007-07-25 19:16 ID:moFgDKky

Although, to be fair, the Perl version isn't using Binet's formula, which the Factor version is: http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html

Name: Anonymous 2007-07-25 19:51 ID:Bzb8abjm

>>26
both versions in >>22 use it.

Name: Anonymous 2007-07-27 8:18 ID:22mT+uXz

>>22
the factor version is actually shorter if you count tokens instead of characters.

Name: Anonymous 2007-07-27 13:41 ID:t7FojlNs

>>27
Whoops. I glanced at it and saw no sqrt and something that looked vaguely recursive.

What's with the + in "return+"?

Name: Anonymous 2007-07-27 19:38 ID:YSh9uO/z

lol wut, the simple haskell version is shorter than both perl and factor, and way more readable than both. actually it's not a haskell hack or anything, just the definition of the function, and i'm betting it looks the same in pretty much every language that's not retarded.
f n = ((1 + sqrt 5) ^^ n - (1 - sqrt 5) ^^ n) / ((2 ^^ n) * sqrt 5)
sub fib{my$n=pop,$f=5**.5;return+((1+$f)**$n-(1-$f)**$n)/($f*2**$n)}
and if you do the perl trick of skipping whitespace, it gets shorter
f n=((1+sqrt 5)^^n-(1-sqrt 5)^^n)/((2^^n)*sqrt 5)

Name: Anonymous 2007-07-27 19:54 ID:YSh9uO/z

scheme:

(define (f n) (/ (- (expt (+ 1 (sqrt 5)) n) (expt (- 1 (sqrt 5)) n)) (sqrt 5) (expt 2 n)))

and when you skip the whitespace...
(define(f n)(/(-(expt(+ 1(sqrt 5))n)(expt(- 1(sqrt 5))n))(sqrt 5)(expt 2 n)))
which is shorter than factor (and way way way more readable, unless you happen to think dup, swap, rot, tuck and nip are better than lotsa parenthesis)
btw, that shuffling macro seems interesting, I for instance can hardly remember what the functions do.

Name: Anonymous 2007-07-27 20:11 ID:Heaven

>>29
http://perldoc.perl.org/perlop.html#Named-Unary-Operators-operator%2c-named-unary

and way way way more readable, unless you happen to think dup, swap, rot, tuck and nip are better than lotsa parenthesis
you'd be surprised how many people do.

also, >>30-31 should count every pair of parentheses as a token.

if you're counting characters, that's just stupid. especially when comparing different languages.

Name: Anonymous 2007-07-27 20:18 ID:t7FojlNs

>>32
Whoa... those named unary operators seem like evil cruft. Why not just use parens properly? Did I miss something?

Name: Anonymous 2007-07-27 20:19 ID:YSh9uO/z

>>32
counting tokens is equally unuseful IMHO, because there is quite a difference in reading shuffling words and a single parenthesis. I think that the versions I proposed are superior (if we don't skip whitespace), because it's a mathematical function and they express it in a mathematical, succint way.
Yes, I know many people prefer dup swap rot tuck to ))))), but I still recon those versions I proposed are more readable.

Name: Anonymous 2007-07-27 20:24 ID:Heaven

>>33
because it's perl and '+' is one character shorter than '()'.

Name: Anonymous 2007-07-27 21:01 ID:Heaven

: fib ( n -- n ) 5 sqrt ab-babb 1 swap - swap 1+ abc-abca ^ abc-acba ^ - abc-cab 2^ * / ;

Name: Anonymous 2007-07-28 8:10 ID:t2HDLMhS

APL
fib ← {⍬⍴⌽∘(+\)⍣⍵,0 1}   
(Bah. http://www.dyalog.dk/dfnsdws/n_fibonacci.htm)
         

Name: Anonymous 2007-07-28 8:37 ID:B6NSBLjB

>>37
Now I know what Perl was modelled after.

Name: Anonymous 2007-07-28 17:14 ID:AkQb87rt

>>37
you should use binet's formula instead.
fib ← (0.5×1+1 ¯1×5*0.5)∘{(-⌿⍺∘.*⍵)÷-/⍺}

Name: Anonymous 2007-08-08 15:18 ID:Dl/FzO8+

From Owl's (Forth knock-off) site:

["\n"]c,"Fibonacci numbers\n"1.c@1 1[%0>][%.c@%2'2'+]!"Done!\n"m

Let's see in detail what happens:
- the ["\n"]c, group stores (,) in c a function whose only scope is emitting a new line ("\n")
- after the title (which is printed), the loop begins (in forth it would be a BEGIN WHILE REPEAT cycle)
- the two functions that manage this cycle are [%0>] (the control syntax sounds like "until the number on top of stack is positive") and [%.c@%2'2'+] (this is the actual algorithm for calculating Fibonacci numbers)
- the cycle is marked by the ! command
- the c@ sequence executes the function in c (new line)
- the 2' sequence is a ROT (namely a 2 ROLL in forth style)
- the symbol % is the DUP command
- the cycle stops when the number on TOS is negative (see the control function [%0>])

This program (actually a line) works on all systems that resolve an integer overflow as a negative number (e.g., for what about 32bit machines, my eMac G4 performs 2147483647 + 1 = -2147483648, which is a perfectly legal number).

The output (on my emac G4 1.42 GHertz) is:

Fibonacci numbers
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
Done!


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