From e4b26b3d6762c8be79c81de4a87efffe8c5e7bfe Mon Sep 17 00:00:00 2001 From: Robby Findler Date: Mon, 7 Jan 2008 14:26:31 +0000 Subject: [PATCH] added something to illustrate the difference between any and any/c svn: r8247 --- .../guide/contracts-simple-function.scrbl | 43 +++++++++++++++++++ collects/scribblings/guide/contracts-utils.ss | 12 ++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/collects/scribblings/guide/contracts-simple-function.scrbl b/collects/scribblings/guide/contracts-simple-function.scrbl index 200723f541..a6d65765d3 100644 --- a/collects/scribblings/guide/contracts-simple-function.scrbl +++ b/collects/scribblings/guide/contracts-simple-function.scrbl @@ -281,3 +281,46 @@ scheme ] +@ctc-section{The difference between @scheme[any] and @scheme[any/c]} + +The contract @scheme[any/c] accepts any value, and +@scheme[any] is a keyword that can appear in the range of +the function contracts (@scheme[->], @scheme[->*], and +@scheme[->d]), so it is natural to wonder what is the +difference between these two contracts: +@schemeblock[ +(-> integer? any) +(-> integer? any/c) +] + +Both allow any result, right? There are two differences: +@itemize{ + +@item{In the first case, the function may return anything at +all, including multiple values. In the second case, the +function may return any value, but not more than one. For +example, this function: +@schemeblock[ +(define (f x) (values (+ x 1) (- x 1))) +] +meets the first contract, but not the second one.} + +@item{Relatedly, this means that a call to a function that +has the first contract is not a tail call. So, for example, +this program is an infinite loop that takes only a constant +amount of space, but if you replace @scheme[any] with +@scheme[any/c], it uses up all of the memory available. + +@schemeblock[ +(module server scheme + (provide/contract + [f (-> (-> procedure? any) boolean?)]) + (define (f g) (g g))) + +(module client scheme + (require 'server) + (f f)) + +(require 'client) +] +}} diff --git a/collects/scribblings/guide/contracts-utils.ss b/collects/scribblings/guide/contracts-utils.ss index 99480bc90e..f9993dca51 100644 --- a/collects/scribblings/guide/contracts-utils.ss +++ b/collects/scribblings/guide/contracts-utils.ss @@ -2,18 +2,22 @@ (require scribble/basic scribble/manual) -(provide question - questionlink +(provide ctc-section question + ctc-link questionlink exercise solution) -(define (question #:tag [tag #f] . rest) +(define (ctc-section #:tag [tag #f] . rest) (keyword-apply section '(#:tag) (list (and tag (str->tag tag))) rest)) -(define (questionlink tag . rest) (apply seclink (str->tag tag) rest)) +(define question ctc-section) + +(define (ctc-link tag . rest) (apply seclink (str->tag tag) rest)) + +(define questionlink ctc-link) (define (str->tag tag) (format "contracts-~a" tag))