Skip to content

Instantly share code, notes, and snippets.

@winny-
Created December 8, 2016 01:36
Show Gist options
  • Save winny-/5988f5fbc894212b0e824477422eccb4 to your computer and use it in GitHub Desktop.
Save winny-/5988f5fbc894212b0e824477422eccb4 to your computer and use it in GitHub Desktop.
#lang typed/racket
(: fib (Exact-Nonnegative-Integer . -> . Exact-Nonnegative-Integer))
(define (fib n)
(cond
[(< n 2) 1]
[else (+ (fib (sub1 n))
(fib (- n 2)))]))
@zenspider
Copy link

(: fib (Integer . -> . Exact-Nonnegative-Integer))
(define (fib n)
  (cond
    [(< n 2) 1]
    [else (+ (fib (cast (sub1 n) Exact-Nonnegative-Integer))
             (fib (cast (- n 2) Exact-Nonnegative-Integer)))]))

@winny-
Copy link
Author

winny- commented Dec 8, 2016

That's wonderful, thanks!

@zenspider
Copy link

(: fib (Integer . -> . Exact-Nonnegative-Integer))
(define (fib n)
  (: -- (-> Integer Exact-Nonnegative-Integer Exact-Nonnegative-Integer))
  (define (-- a b) (cast (- a b) Exact-Nonnegative-Integer))
  (cond
    [(< n 2) 1]
    [else (+ (fib (-- n 1))
             (fib (-- n 2)))]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment