[racket] Exploratory programming?

From: Markku Rontu (markku.rontu at iki.fi)
Date: Thu Dec 2 12:46:11 EST 2010

On Thu, 2010-12-02 at 12:26 -0500, Greg Hendershott wrote:
> Although I've grown accustomed to it, early on I disliked the
> redundancy of define/contract and (my own macro)
> define/contract/provide.
> 
> (define/contract (foo x #:y y)
>   (int? #:y boolean?)
>   ( ... body ... ))

It's interesting to see what other people do.

This is what I have played with. I was interested in adding
documentation to everything, and make it proper quality. Instead of
using comments I want the documentation to have a defined format that
can be exploited, and be part of the language I use with good support. I
don't want to write separate manuals at this point yet. 

My style includes function signature, contract, documentation (first
string is brief text, rest strings are separate paragraphs) and a set of
unit tests which also form the set of examples given with the "help". I
haven't included Typed Racket in this but ideally it would be included
too, not just contracts like I have now.

-Markku

---

(define/contract/help (mul lon)
  ((listof number?) . -> . number?)
  ("Multiplies a list of numbers"
   "The function mul multiplies together the given list of numbers. The
mul of an empty list is 1.")
  (((mul '()) -> 1)
   ((mul '(1)) -> 1)
   ((mul '(1 2 3 4)) -> 24))
  (if (null? lon)
      1
      (* (first lon)
         (mul (rest lon)))))

(define/contract/help (string-shorten s max-length #:replacement
(replacement "..."))
  ((string? number?) (#:replacement string?). ->* . string?)
  ("Shortens a string from the right"
   "The function string-shorten trims the given string from the right
and replaces extra characters with the given replacement which is ... by
default")
  (((string-shorten "word" 4) -> "word")
   ((string-shorten "word" 2) -> "wo")
   ((string-shorten "word" 2 #:replacement "x") -> "wx")
   ((string-shorten "word" 3 #:replacement "x") -> "wox")
   ((string-shorten "aaabbbcccdddeeefff" 5) -> "aa..."))
  (let ((sl (string-length s)))
    (if (<= sl max-length)
        s
        (let* ((rl (string-length replacement))
               (leave (- (min sl max-length) rl)))
          (if (< leave 0)
              (substring s 0 max-length)
              (string-append (substring s 0 leave) replacement))))))




Posted on the users mailing list.