<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>If I were making something like case-lambda/contract, which would be better/make more sense?</div><div><br></div><div><div><font class="Apple-style-span" face="'Courier New'"><div>#lang racket</div><div><br></div><div>(require test-engine/racket-tests)</div><div><br></div><div>;; this one takes a contract within each case, and puts in in a case-> for you:</div><div>(define-syntax-rule (case-lambda/contract-1 [args c e0 e ...] ...)</div><div> (let ()</div><div> (define/contract f</div><div> (case-> c ...)</div><div> (case-lambda </div><div> [args e0 e ...]</div><div> ...))</div><div> f))</div><div><br></div><div>;; example for case-lambda/contract-1:</div><div>(define report-cost-1</div><div> (case-lambda/contract-1</div><div> [(lo hi) (integer? integer? . -> . string?)</div><div> (format "between $~a and $~a" lo hi)]</div><div> [(desc) (string? . -> . string?)</div><div> (format "~a of dollars" desc)]))</div><div><br></div><div>(check-expect (report-cost-1 5 8)</div><div> "between $5 and $8")</div><div>(check-expect (report-cost-1 "millions")</div><div> "millions of dollars")</div><div>(check-error (report-cost-1 "thousands" "millions"))</div><div>(check-error (report-cost-1 5))</div><div><br></div><div><br></div><div>;; this one takes a contract for the whole thing, and lets you do the case-> yourself:</div><div>(define-syntax-rule (case-lambda/contract-2 contract-expr [args e0 e ...] ...)</div><div> (let ()</div><div> (define/contract f</div><div> contract-expr</div><div> (case-lambda </div><div> [args e0 e ...]</div><div> ...))</div><div> f))</div><div><br></div><div>;; example for case-lambda/contract-2:</div></font><div><div style="font-family: 'Courier New'; ">(define report-cost-2</div><div style="font-family: 'Courier New'; "> (case-lambda/contract-2</div><div style="font-family: 'Courier New'; "> (case-></div><div style="font-family: 'Courier New'; "> (integer? integer? . -> . string?)</div><div style="font-family: 'Courier New'; "> (string? . -> . string?))</div><div style="font-family: 'Courier New'; "> [(lo hi) (format "between $~a and $~a" lo hi)]</div><div style="font-family: 'Courier New'; "> [(desc) (format "~a of dollars" desc)]))</div></div><div><br></div><font class="Apple-style-span" face="'Courier New'"><div>(check-expect (report-cost-2 5 8)</div><div> "between $5 and $8")</div><div>(check-expect (report-cost-2 "millions")</div><div> "millions of dollars")</div><div>(check-error (report-cost-2 "thousands" "millions"))</div><div>(check-error (report-cost-2 5))</div><div><br></div><div><br></div><div>(test)</div></font></div></div><div><br></div><div><br></div><div>Which one is better/makes more sense?</div><div><br></div><div><div style="font-family: 'Courier New'; ">(define report-cost-1</div><div style="font-family: 'Courier New'; "> (case-lambda/contract-1</div><div style="font-family: 'Courier New'; "> [(lo hi) (integer? integer? . -> . string?)</div><div style="font-family: 'Courier New'; "> (format "between $~a and $~a" lo hi)]</div><div style="font-family: 'Courier New'; "> [(desc) (string? . -> . string?)</div><div style="font-family: 'Courier New'; "> (format "~a of dollars" desc)]))</div></div><div><br></div><div><div style="font-family: 'Courier New'; ">(define report-cost-2</div><div style="font-family: 'Courier New'; "> (case-lambda/contract-2</div><div style="font-family: 'Courier New'; "> (case-></div><div style="font-family: 'Courier New'; "> (integer? integer? . -> . string?)</div><div style="font-family: 'Courier New'; "> (string? . -> . string?))</div><div style="font-family: 'Courier New'; "> [(lo hi) (format "between $~a and $~a" lo hi)]</div><div style="font-family: 'Courier New'; "> [(desc) (format "~a of dollars" desc)]))</div></div><div><br></div><div><br></div><div>On Nov 17, 2013, at 12:43 PM, Matthias Felleisen wrote:</div></div></div></div></div><div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>I would choose option 2, because I have a strong preference for seeing specs first and ignoring implementations if I can. You may wish to ask on the list; others may have other ideas. -- Matthias</div><div><br></div><div><br></div><div><br></div><div><br></div><br><div><div>On Nov 16, 2013, at 11:05 AM, Alexander D. Knauth wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>If I were making something like this for case-lambda, which would be better/make more sense?</div><div><br></div><div><div><font class="Apple-style-span" face="'Courier New'"><div>#lang racket</div><div><br></div><div>(require test-engine/racket-tests)</div><div><br></div><div>;; this one takes a contract within each case, and puts in in a case-> for you:</div><div>(define-syntax-rule (case-lambda/contract-1 [args c e0 e ...] ...)</div><div> (let ()</div><div> (define/contract f</div><div> (case-> c ...)</div><div> (case-lambda </div><div> [args e0 e ...]</div><div> ...))</div><div> f))</div><div><br></div><div>;; example for case-lambda/contract-1:</div><div>(define report-cost-1</div><div> (case-lambda/contract-1</div><div> [(lo hi) (integer? integer? . -> . string?)</div><div> (format "between $~a and $~a" lo hi)]</div><div> [(desc) (string? . -> . string?)</div><div> (format "~a of dollars" desc)]))</div><div><br></div><div>(check-expect (report-cost-1 5 8)</div><div> "between $5 and $8")</div><div>(check-expect (report-cost-1 "millions")</div><div> "millions of dollars")</div><div>(check-error (report-cost-1 "thousands" "millions"))</div><div>(check-error (report-cost-1 5))</div><div><br></div><div><br></div><div>;; this one takes a contract for the whole thing, and lets you do the case-> yourself:</div><div>(define-syntax-rule (case-lambda/contract-2 contract-expr [args body ...] ...)</div><div> (let ()</div><div> (define/contract f</div><div> contract-expr</div><div> (case-lambda </div><div> [args body ...]</div><div> ...))</div><div> f))</div><div><br></div><div>;; example for case-lambda/contract-2:</div></font><div><div style="font-family: 'Courier New'; ">(define report-cost-2</div><div style="font-family: 'Courier New'; "> (case-lambda/contract-2</div><div style="font-family: 'Courier New'; "> (case-></div><div style="font-family: 'Courier New'; "> (integer? integer? . -> . string?)</div><div style="font-family: 'Courier New'; "> (string? . -> . string?))</div><div style="font-family: 'Courier New'; "> [(lo hi) (format "between $~a and $~a" lo hi)]</div><div style="font-family: 'Courier New'; "> [(desc) (format "~a of dollars" desc)]))</div></div><div><br></div><font class="Apple-style-span" face="'Courier New'"><div>(check-expect (report-cost-2 5 8)</div><div> "between $5 and $8")</div><div>(check-expect (report-cost-2 "millions")</div><div> "millions of dollars")</div><div>(check-error (report-cost-2 "thousands" "millions"))</div><div>(check-error (report-cost-2 5))</div><div><br></div><div><br></div><div>(test)</div></font></div></div><div><br></div><div><br></div><div>Which one is better/makes more sense?</div><div><br></div><div><div style="font-family: 'Courier New'; ">(define report-cost-1</div><div style="font-family: 'Courier New'; "> (case-lambda/contract-1</div><div style="font-family: 'Courier New'; "> [(lo hi) (integer? integer? . -> . string?)</div><div style="font-family: 'Courier New'; "> (format "between $~a and $~a" lo hi)]</div><div style="font-family: 'Courier New'; "> [(desc) (string? . -> . string?)</div><div style="font-family: 'Courier New'; "> (format "~a of dollars" desc)]))</div></div><div><br></div><div><div style="font-family: 'Courier New'; ">(define report-cost-2</div><div style="font-family: 'Courier New'; "> (case-lambda/contract-2</div><div style="font-family: 'Courier New'; "> (case-></div><div style="font-family: 'Courier New'; "> (integer? integer? . -> . string?)</div><div style="font-family: 'Courier New'; "> (string? . -> . string?))</div><div style="font-family: 'Courier New'; "> [(lo hi) (format "between $~a and $~a" lo hi)]</div><div style="font-family: 'Courier New'; "> [(desc) (format "~a of dollars" desc)]))</div></div><div><br></div><div><br></div><div>On Nov 13, 2013, at 2:45 PM, Matthias Felleisen wrote:</div><div><br class="Apple-interchange-newline"><blockquote type="cite"><div><br>Do you want something like this :<br><br>#lang racket<br><br>(define-syntax-rule <br> (lambda/contract (x ...) c e0 e ...)<br> ;; ==> <br> (let ()<br> (define/contract (f x ...) c e0 e ...)<br> f))<br><br>;; -----------------------------------------------------------------------------<br><br>(define g (lambda/contract (x) (integer? . -> . integer?) (* pi x)))<br><br>(void (= (g 0) 0))<br>(with-handlers ([exn:fail:contract? void])<br> (g 1))<br><br><br><br><br><br>On Nov 13, 2013, at 1:12 PM, Matthew Butterick <<a href="mailto:mb@mbtype.com">mb@mbtype.com</a>> wrote:<br><br><blockquote type="cite">Having gotten in the habit of writing function contracts, I prefer define/contract to (provide (contract-out ...)) because it keeps the contract near the function.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Is there an analogous idiom for lambda? I see there is no lambda/contract, and the docs on contracts for case-lambda [1] uses the (provide (contract-out ...)) style.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">[1] <a href="http://docs.racket-lang.org/guide/contracts-general-functions.html#(part._contracts-case-lambda)">http://docs.racket-lang.org/guide/contracts-general-functions.html#(part._contracts-case-lambda)</a><br></blockquote><blockquote type="cite">____________________<br></blockquote><blockquote type="cite"> Racket Users list:<br></blockquote><blockquote type="cite"> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote><br><br>____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></div></blockquote></div><br></div></blockquote></div><br></div></blockquote></div><br></body></html>