[racket] Signature for an optional-argument function?
Although I don't recall for HtDP, the convention I've seen in Racket
and docs generally is to use square brackets for an optional argument
in the sense you mean: A single argument that's present or not. For
instance:
;; ArgType [OptionalArgType] ArgType -> ResultType
Whereas I would read "foo ..." as "zero or more": 0, 1, 2, .... +inf
But your optional argument example is really "zero or one" (not more).
A cheat sheet:
Function/syntax sigs in docs:
[zero-or-one]
zero-or-more ...
Regular expressions:
zero-or-one?
zero-or-more*
p.s. When it comes to ONE-or-more in things like regexps, match
patterns, macro syntax rules, and docs, there's more variety.
Sometimes there's no explicit one-or-more notation so you have to
"fake it" as:
foo0 foo1 ... ;; e.g. syntax-rules, syntax-case
But sometimes you'll find notation like:
foo+ ;; e.g. regexp
foo ...+ ;; e.g. match, syntax-parse
On Fri, Aug 23, 2013 at 12:28 AM, Alexander D. Knauth
<alexander at knauth.org> wrote:
> 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?
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>