<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Aug 23, 2013, at 12:28 AM, Alexander D. Knauth wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div 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<div><br></div><div>I was wondering if I could define a function with optional arguments</div></div></blockquote><div><br></div><div><br></div><div>In ISL+ you cannot defined variable-arity functions. In Racket there are many mechanism for doing so (all reduced to one in the core language). You can simulate the idea in ISL+, which you might have done. </div><div><br></div><div><br></div><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div> So how do I write the signature? </div></div></blockquote><div><br></div><div>In ISL+, that depends on how you simulate it. The best way is to define a function that consumes a list of values but expects specific kinds of values. For your flexible fold below, you could write: </div><div><br></div><div>;; [List (X Y -> Y) (Z -> X) Y [List-of Z]] </div><div>;; or</div><div>;; [List (X Y -> Y) Y [List-of Z]] </div><div>;; -> Y</div><div><br></div><div>If you do not like 'or', introduce a separate data definition like this: </div><div><br></div><div>;; [FoldArgument X Y Z] is one of: </div><div>;; -- [List (X Y -> Y) (Z -> X) Y [List-of Z]] </div><div>;; -- [List (X Y -> Y) Y [List-of Z]] </div><div><br></div><div>and then you write </div><div><br></div><div>;; [FoldArgument X Y Z] -> Y </div><div><br></div><div>In Racket, we tend to use square brackets in documentation. In contracts -- a checked form of signature for module boundaries -- we have a notation for this idea. </div><div><br></div><div><br></div><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><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><br></div></span></div></blockquote><div><br></div><div>I would run your example as </div><div><br></div><div> (fold (compose + string-length) 0 '("a" ...)) </div><div><br></div><div>and be done. </div><div><br></div><br><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><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></blockquote><br></div><div>For fun, you can write any kind of function you like. </div><div><br></div><div>As long as you design these functions to match the structure of the data definition, your fellow programmers won't mind. Otherwise, they will not like you. See my home page re 'psychopath'. </div><div><br></div><div>-- Matthias</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><br></body></html>