<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
<br>
-------- Originele bericht --------
<table class="moz-email-headers-table" border="0" cellpadding="0"
cellspacing="0">
<tbody>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Onderwerp:
</th>
<td>Re: [racket] Is this a good design</td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Datum: </th>
<td>Thu, 01 Mar 2012 10:05:25 +0100</td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Van: </th>
<td>Roelof Wobben <a class="moz-txt-link-rfc2396E" href="mailto:r.wobben@home.nl"><r.wobben@home.nl></a></td>
</tr>
<tr>
<th align="RIGHT" nowrap="nowrap" valign="BASELINE">Aan: </th>
<td>David Van Horn <a class="moz-txt-link-rfc2396E" href="mailto:dvanhorn@ccs.neu.edu"><dvanhorn@ccs.neu.edu></a></td>
</tr>
</tbody>
</table>
<br>
<br>
<pre>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") ))
Roelof
</pre>
</body>
</html>