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-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