File: fibo.l

package info (click to toggle)
picolisp 3.1.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,100 kB
  • sloc: ansic: 14,205; lisp: 795; makefile: 290; sh: 13
file content (50 lines) | stat: -rw-r--r-- 930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 10jul11abu
# (c) Software Lab. Alexander Burger

# Standard version
(de fibo (N)
   (if (> 2 N)
      1
      (+ (fibo (dec N)) (fibo (- N 2))) ) )


# Parallelized version
(de fibo+ (D N)  # Uses 2**D processes
   (cond
      ((> 1 (dec 'N)) 1)
      ((ge0 (dec 'D))
         (let (A NIL B NIL)
            (later 'A (fibo+ D N))
            (later 'B (fibo+ D (dec N)))
            (wait NIL (and A B))
            (+ A B) ) )
      (T
         (+
            (fibo+ D N)
            (fibo+ D (dec N)) ) ) ) )


# Using a cache (fastest)
(de cachedFibo (N)
   (cache '(NIL) (pack (char (hash N)) N)
      (if (> 2 N)
         1
         (+ (cachedFibo (dec N)) (cachedFibo (- N 2))) ) ) )


# Coded in 'C'
`(== 64 64)  # Only in the 64-bit version

(load "@lib/native.l")

(gcc "fibo" NIL
   (cFibo (N) "Fibo" 'I N) )

int Fibo(int n) {
   if (n < 2)
      return 1;
   return Fibo(n-1) + Fibo(n-2);
}
/**/

# vi:et:ts=3:sw=3