picolisp

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/picolisp.git/
Log | Files | Refs | README | LICENSE

fibo.l (934B)


      1 # 27oct12abu
      2 # (c) Software Lab. Alexander Burger
      3 
      4 # Standard version
      5 (de fibo (N)
      6    (if (>= 2 N)
      7       1
      8       (+ (fibo (dec N)) (fibo (- N 2))) ) )
      9 
     10 
     11 # Parallelized version
     12 (de fibo+ (D N)  # Uses 2**D processes
     13    (cond
     14       ((>= 1 (dec 'N)) 1)
     15       ((ge0 (dec 'D))
     16          (let (A NIL B NIL)
     17             (later 'A (fibo+ D N))
     18             (later 'B (fibo+ D (dec N)))
     19             (wait NIL (and A B))
     20             (+ A B) ) )
     21       (T
     22          (+
     23             (fibo+ D N)
     24             (fibo+ D (dec N)) ) ) ) )
     25 
     26 
     27 # Using a cache (fastest)
     28 (de cachedFibo (N)
     29    (cache '(NIL) (pack (char (hash N)) N)
     30       (if (>= 2 N)
     31          1
     32          (+ (cachedFibo (dec N)) (cachedFibo (- N 2))) ) ) )
     33 
     34 
     35 # Coded in 'C'
     36 `(== 64 64)  # Only in the 64-bit version
     37 
     38 (load "@lib/native.l")
     39 
     40 (gcc "fibo" NIL
     41    (cFibo (N) "Fibo" 'I N) )
     42 
     43 int Fibo(int n) {
     44    if (n <= 2)
     45       return 1;
     46    return Fibo(n-1) + Fibo(n-2);
     47 }
     48 /**/
     49 
     50 # vi:et:ts=3:sw=3