Skip to content

Instantly share code, notes, and snippets.

@cesarmarinhorj
Forked from Olical/fib.rkt
Created June 30, 2024 15:43
Show Gist options
  • Save cesarmarinhorj/a24fd1b576eea7de67529d55def40475 to your computer and use it in GitHub Desktop.
Save cesarmarinhorj/a24fd1b576eea7de67529d55def40475 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