[racket] Fwd: Re: Is this a good design

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Thu Mar 1 17:11:06 EST 2012

On 3/1/12 4:45 PM, Roelof Wobben wrote:
>
>
> -------- Originele bericht --------
> Onderwerp: 	Re: [racket] Is this a good design
> Datum: 	Thu, 01 Mar 2012 10:05:25 +0100
> Van: 	Roelof Wobben <r.wobben at home.nl>
> Aan: 	David Van Horn <dvanhorn at ccs.neu.edu>
>
>
>
> You can imagine alternative statements of the program contract and
>>>>>>  purpose that would lead to different code. One is to drop the
>>>>>>  non-empty assumption and state the error case:
>>>>>>
>>>>>>  ;; String ->  String
>>>>>>  ;; Get first character of string if there is one, error otherwise
>>>>>>
>>>>>>  Then you'd want to specify the error behavior in examples (and write
>>>>>>  tests that tested this behavior) and make sure your code changed to
>>>>>>  reflect the revised class of data it consumes.
>>>>>>
>
> Oke,
> Because the contract says the input always be a string so I only have to
> check on empty strings because then there is no first character.  So the
> function will be :
> ;; String ->  String
> ;; Print a error message when the input is wrong.
> (define (print-foutmelding s)
>    (string-append "Error:" s " on the function first-string"))
>
> ;; String ->  String
> ;; Get first character of string (s) if there is one, error otherwise
> ;; given "aaa" expected "a"
> ;; given "" expected " Error: Empty string  found on the function first
> string"
> (define (first-string s)
>    (if (>  (string-length s) 0) (string-ith s 0) (print-foutmelding
> "Empty string found")))
>
>
> Yet another is:
>>>>>>  ;; Any ->  String
>>>>>>  ;; Get first character if given a non-empty string, error otherwise
>>>>>>
>
> Because the input can be anything and only on strings the first
> character can be found I have to do two checks.
> On is the input a string and second is it a non-empty string.
> So the function will look like this :
>
> ;;String ->  String
> ;; Put the error message on screen because it's a empty string.
> ;; given "Empty string" expect ""Error:Empty string found on the
> function first-string"
> ;; given "No String found" expect ""Error:No string found on the
> function first-string"
> (define (print-foutmelding s)
>    (string-append "Error:" s " on the function first-string"))
>
> ;; Any  ->  String
> ;; Get first character of string (s) if there is one, error otherwise
> ;; given "aaa" expected "a"
> ;; given "" expected " Error: Empty string  on the function first-string"
> ;; given "2" expected "Error : No string found on the function first-string
> (define (first-string s)
>    (if (string? s) (if (>  (string-length s) 0) (string-ith s 0)
> (print-foutmelding "Empty string found"))(print-foutmelding "No string
> found") ))

If your program should signal errors, you should call the error 
function; not produce a string.

David

Posted on the users mailing list.