[racket] Signature for an optional-argument function?

From: Alexander D. Knauth (alexander at knauth.org)
Date: Fri Aug 23 00:28:17 EDT 2013

Right now I'm using Intermediate Student Language, not Racket, because  
I don't know Racket and I've been taking Introduction to Systematic  
Program Design - Part I online, which is based on HtDP, or How to  
Design Programs.

I was wondering if I could define a function with optional arguments,  
and I figured out how to define them, but since they accept different  
numbers of arguments, the signature would be different depending on  
how many arguments it's called with.  So how do I write the signature?

Would it be something like this, with a "one of"?:
;; one of:
;; FirstArgumentType SecondArgumentType ... OptionalArgumentType ... - 
 > ResultType
;; FirstArgumentType SecondArgumentType ... -> ResultType

Or would it be something like this, with a question mark?:
;; FirstArgumentType SecondArgumentType ... OptionalArgumentType? ... - 
 > ResultType

Or should it be something else entirely?

The particular example I'm thinking of is an abstract fold function  
that has an optional argument (a function) for the contribution of the  
first:
(check-expect (fold + 0 (list 1 2 3)) 6)           ;sum, without  
optional argument
(check-expect (fold + identity 0 (list 1 2 3)) 6)  ;sum, with identity  
as the optional argument
(check-expect (fold + string-length 0 (list "a" "bc" "def"))  
6)  ;total-string-length

(define fold
   (local [(define (fold-with-cof comb cof base lox)
             (foldr comb base (map cof lox)))
           (define (fold--args args)
             (cond [(= 3 (length args)) (apply foldr args)]
                   [(= 4 (length args)) (apply fold-with-cof args)]
                   [else
                    (error "fold: expects 3 or 4 arguments, but found  
" (length args))]))]
     (compose fold--args list)))

You could also design a weird function that does completely different  
things depending on what the arguments are, like this:

(check-expect (foo "string" 2 6) "ring")
(check-expect (foo 1 2 3 4 5) 15)
(check-expect (foo 9 empty true) (list false true false))

(define foo
   (local [(define (fold--args args)
             (cond [(and (or (= 2 (length args))
                             (and (= 3 (length args))
                                  (number? (third args))))
                         (string? (first args))
                         (number? (second args)))
                    (apply substring args)]
                   [(andmap number? args)
                    (apply + args)]
                   [else
                    (map empty? args)]))]
     (compose foo--args list)))

How would I write the signature for a weird function like this?  Or  
should such a weird function not even exist?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130823/a27eaf2e/attachment-0001.html>

Posted on the users mailing list.