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

From: Roelof Wobben (r.wobben at home.nl)
Date: Fri Mar 2 02:05:36 EST 2012

Op 1-3-2012 23:11, David Van Horn schreef:
> 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
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
>

I understand but that part is not yet discussed in the book. Catching 
error messags is in the last chapter and I now studying one of the first 
chapters.

Roelof


Posted on the users mailing list.