Skip to content

Instantly share code, notes, and snippets.

@winny-
Created March 5, 2018 19:31
Show Gist options
  • Save winny-/5f2c199dc83d40f486cbf66e0ab12fd9 to your computer and use it in GitHub Desktop.
Save winny-/5f2c199dc83d40f486cbf66e0ab12fd9 to your computer and use it in GitHub Desktop.
1
120
==== but the following will fail ====
fact: contract violation
expected: natural?
given: -5
in: the 1st argument of
(-> natural? natural?)
contract from: (function fact)
blaming: /tmp/ex.rkt
(assuming the contract is correct)
at: /tmp/ex.rkt:3.18
==== this runs because the above exception is caught ====
#lang racket
(define/contract (fact n)
(-> exact-nonnegative-integer? exact-nonnegative-integer?)
(if (zero? n)
1
(* n (fact (sub1 n)))))
(displayln (fact 0))
(displayln (fact 5))
(displayln "==== but the following will fail ====")
(displayln (with-handlers ([exn:fail:contract? exn-message])
(fact -5)))
(displayln "==== this runs because the above exception is caught ====")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment