Skip to content

Instantly share code, notes, and snippets.

@Olical
Last active September 27, 2016 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Olical/3b4688256481086f53549355b3f9b930 to your computer and use it in GitHub Desktop.
Save Olical/3b4688256481086f53549355b3f9b930 to your computer and use it in GitHub Desktop.
A fast, recursive, typed fibonacci program written in Racket.
#lang typed/racket
(: fib (-> Number Number))
(define (fib n)
(fib-iter 0 1 n))
(: fib-iter (-> Number Number Number Number))
(define (fib-iter a b n)
(if (= n 0) a
(fib-iter b (+ a b) (- n 1))))
(define nth (command-line #:args (#{nth : String}) (string->number nth)))
(if (number? nth)
(fib nth)
(displayln "Please provide a number as an argument."))
time -p racket fib.rkt 100000 | wc -c
real 0.66
user 0.59
sys 0.06
20900

Not too shabby considering typed/racket incurs quite an overhead. This is after compiling to bytecode.

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