[racket] Fwd: Re: Is this a good design
Op 2-3-2012 8:05, Roelof Wobben schreef:
> 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
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>
I did some study on that chapter but I cannnot make it work,
This is what I have so far :
;;String -> String
;; Get the first character of a string (s) if there is one. error otherwise.
;; given "aaaa" expected "a"
;; given "" expected "
(define (first-string s)
(cond
[(eq? s "") (error "first-string" "Empty string found")] ;;
check for a empty string.if so put a error message
[(and (string? s) (not(eq? s ""))) (string-ith s 0)]
;; if it's a non-empty string then do the trick.
[(number? s) (error "first-string" "No string found")] ;; if
it's a number then put a error message
[(boolean? s) (error "first-string" "No string found")] ;; if
it's a boolean then put a error message
[(image? s) (error "first string" "No string found")] ;; if
it's a image put a error message
))
But when I do (first-string ") then still DrRacket gives his own error
message instead of the one I made.
Any tips are welcome here.
Roelof