<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">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. <div><br></div><div>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? </div><div><br></div><div>Would it be something like this, with a "one of"?:</div><div><font class="Apple-style-span" face="Monaco">;; one of: </font></div><div><font class="Apple-style-span" face="Monaco">;; FirstArgumentType SecondArgumentType ... </font><span class="Apple-style-span" style="font-family: Monaco; ">OptionalArgumentType ...</span><span class="Apple-style-span" style="font-family: Monaco; "> </span><span class="Apple-style-span" style="font-family: Monaco; ">-> ResultType</span></div><div><font class="Apple-style-span" face="Monaco">;; FirstArgumentType SecondArgumentType ... -> ResultType</font></div><div><br></div><div>Or would it be something like this, with a question mark?:</div><div><font class="Apple-style-span" face="Monaco">;; FirstArgumentType SecondArgumentType ... OptionalArgumentType? ... -> ResultType</font></div><div><br></div><div>Or should it be something else entirely?</div><div><br></div><div>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: </div><span class="Apple-style-span" style="font-family: Monaco; "><div><span class="Apple-style-span" style="font-family: Monaco; ">(check-expect (fold + 0 (list 1 2 3)) 6) ;sum, without optional argument</span></div></span><span class="Apple-style-span" style="font-family: Monaco; "><div><span class="Apple-style-span" style="font-family: Monaco; ">(check-expect (fold + identity 0 (list 1 2 3)) 6) ;sum, with identity as the optional argument</span></div></span><span class="Apple-style-span" style="font-family: Monaco; "><div><span class="Apple-style-span" style="font-family: Monaco; ">(check-expect (fold + string-length 0 (list "a" "bc" "def")) 6) ;total-string-length</span></div></span><span class="Apple-style-span" style="font-family: Monaco; "><span class="Apple-style-span" style="font-family: Monaco; "></span></span><span class="Apple-style-span" style="font-family: Monaco; "><div><span class="Apple-style-span" style="font-family: Monaco; "><br></span></div>(defin</span><span class="Apple-style-span" style="font-family: Monaco; ">e</span><span class="Apple-style-span" style="font-family: Monaco; "> fold</span><div><span class="Apple-style-span" style="font-family: Monaco; "> (local [(define (fold-with-cof comb cof base lox)</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (foldr comb base (map cof lox)))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (define (fold--args args)</span><br><span class="Apple-style-span" style="font-family: Monaco; "> (cond [(= 3 (length args)) (apply foldr args)]</span><br><span class="Apple-style-span" style="font-family: Monaco; "> [(= 4 (length args)) (apply fold-with-cof args)]</span><br><span class="Apple-style-span" style="font-family: Monaco; "> [else</span><br></div><span class="Apple-style-span" style="font-family: Monaco; "> (error "fold: expects 3 or 4 arguments, but found " (length args))]))]</span><br><div><div><div><pre style="white-space: pre-wrap; word-wrap: break-word; "><span class="Apple-style-span" style="font-family: Monaco; white-space: normal; "> (compose fold--args list)))</span></pre></div></div></div><div><div><div><br></div></div><div>You could also design a weird function that does completely different things depending on what the arguments are, like this:</div></div><div><br></div><div><span class="Apple-style-span" style="font-family: Monaco; ">(check-expect (foo "string" 2 6) "ring")</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">(check-expect (foo 1 2 3 4 5) 15)</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">(check-expect (foo 9 empty true) (list false true false))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "><div><span class="Apple-style-span" style="font-family: Monaco; "><br></span></div></span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">(defin</span><span class="Apple-style-span" style="font-family: Monaco; ">e</span><span class="Apple-style-span" style="font-family: Monaco; "> foo</span><div><span class="Apple-style-span" style="font-family: Monaco; "> (local [</span><span class="Apple-style-span" style="font-family: Monaco; ">(define (fold--args args)</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (cond [(and </span><span class="Apple-style-span" style="font-family: Monaco; ">(or (= 2 (length args))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (and (= 3 (length args))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (number? (third args))))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (string? (first args))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (number? (second args))</span><span class="Apple-style-span" style="font-family: Monaco; ">)</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (apply substring args)]</span><br><span class="Apple-style-span" style="font-family: Monaco; "> [(andmap number? args) </span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (apply + args)]</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> [else</span></div><div><span class="Apple-style-span" style="font-family: Monaco; "> (map empty? args)]))]</span></div><div><div><div><pre style="white-space: pre-wrap; word-wrap: break-word; "><span class="Apple-style-span" style="font-family: Monaco; white-space: normal; "> (compose foo--args list)))</span></pre></div></div></div><div><div><div><br></div></div><div>How would I write the signature for a weird function like this? Or should such a weird function not even exist? </div></div></div><div><br></div></body></html>