<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. &nbsp;<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. &nbsp;So how do I write the signature? &nbsp;</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:&nbsp;</font></div><div><font class="Apple-style-span" face="Monaco">;; FirstArgumentType SecondArgumentType ...&nbsp;</font><span class="Apple-style-span" style="font-family: Monaco; ">OptionalArgumentType ...</span><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp;</span><span class="Apple-style-span" style="font-family: Monaco; ">-&gt; ResultType</span></div><div><font class="Apple-style-span" face="Monaco">;; FirstArgumentType SecondArgumentType ... -&gt; 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? ... -&gt; 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: &nbsp;</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) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;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) &nbsp;;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) &nbsp;;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; ">&nbsp;fold</span><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; (local [(define (fold-with-cof comb cof base lox)</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (foldr comb base (map cof lox)))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (define (fold--args args)</span><br><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (cond [(= 3 (length args)) (apply foldr args)]</span><br><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(= 4 (length args)) (apply fold-with-cof args)]</span><br><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else</span><br></div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(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; ">&nbsp; &nbsp; (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; ">&nbsp;foo</span><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; (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; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (cond [(and&nbsp;</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; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (and (= 3 (length args))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(number? (third args))))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (string? (first args))</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (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; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(apply substring args)]</span><br><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(andmap number? args) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(apply + args)]</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else</span></div><div><span class="Apple-style-span" style="font-family: Monaco; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(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; ">&nbsp; &nbsp; (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? &nbsp;Or should such a weird function not even exist? &nbsp;</div></div></div><div><br></div></body></html>