Paste number 57452: Fib Calculation

Index of paste annotations: 2 | 1

Paste number 57452: Fib Calculation
Pasted by: phax
When:1 year, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+18BW
Channel:#lisp
Paste contents:
Raw Source | XML | Display As
; Saves all results in a list
(define (fibcalc1 x)
  ( if ( = x 1 ) 
       (cons 1 ())
       ( if ( = x 2 ) (list 1 1)
	    ( let ((temp ( fibcalc1( - x 1 )) ))
	      ( cons ( + ( car temp)  (cadr temp)) temp ))
	    )))



; Saves only last 2 results using a pair
(define (fibcalc2 x)
( if ( = x 1 ) 
     (cons 1 ())
     ( if ( = x 2 ) 
	  (cons 1 1)
	  ( let ((temp ( fibcalc2( - x 1 )) ))
	    ( cons ( + ( car temp)  (cdr temp)) ( car temp))
	    ))))


; Saves only last 2 results using a list
(define (fibcalc3 x)
( if ( = x 1 ) 
     (list 1 )
     ( if ( = x 2 ) 
	  (list 1 1)
	  ( let ((temp ( fibcalc3( - x 1 )) ))
	    ( list ( + ( car temp)  (cadr temp)) ( car temp))
	    ))))

Annotations for this paste:

Annotation number 2: using local functions
Pasted by: Daemmerung
When:1 year, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+18BW#2
Paste contents:
Raw Source | Display As
(define (fib x)
  (define (fibseries x)
    (cond
     ((= x 1) '(1))
     ((= x 2) '(1 1))
     (else (let ((temp (fibseries (- x 1)))) (cons (+ (car temp) (cadr temp)) temp)))))
  (if (or (not (integer? x)) (< x 1))
      (error "fib: argument must be a positive integer, given" x)
      (car (fibseries x))))

Annotation number 1: formatting
Pasted by: rudybot
When:1 year, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+18BW#1
Paste contents:
Raw Source | Display As
(define (fibcalc1 x)
  (cond ((= x 1) (cons 1 '()))
        ((= x 2) (list 1 1))
        (else
         (let ((temp (fibcalc1 (- x 1))))
           (cons (+ (car temp) (cadr temp)) temp)))))

(define (fibcalc2 x)
  (cond ((= x 1) (cons 1 '()))
        ((= x 2) (cons 1 1))
        (else
         (let ((temp (fibcalc2 (- x 1))))
           (cons (+ (car temp) (cdr temp)) (car temp))))))

(define (fibcalc3 x)
  (cond ((= x 1) (list 1))
        ((= x 2) (list 1 1))
        (else
         (let ((temp (fibcalc3 (- x 1))))
           (list (+ (car temp) (cadr temp)) (car temp))))))

Colorize as:
Show Line Numbers
Index of paste annotations: 2 | 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.