<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>Yes, you need two functions: one that gives you the name of a procedure and one that picks a procedure. I was merely hinting at how to do the former. -- Matthias</div><div><br></div><div><br></div><br><div><div>On Jan 4, 2014, at 4:57 PM, Rian Shams wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div>Thanks Matthias,</div><div><br></div>If I use your definition:<div><br></div><div><div style="font-family:arial,sans-serif;font-size:13px">(define (select-random-safe-function)</div><div style="font-family:arial,sans-serif;font-size:13px">


  (object-name </div><div style="font-family:arial,sans-serif;font-size:13px">   (list-ref safe-function-set (random (length safe-function-set)))))</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div>

<div style="font-family:arial,sans-serif;font-size:13px">
then,</div><div><br><div>>(procedure-arity (select-random-safe-function)) </div><div><div>error:procedure-arity: contract violation</div><div>  expected: procedure?</div><div>  given: '$+</div></div></div><div><br>


</div><div><br></div><div>I believe I need two things:</div><div> </div><div>1. (select-random-safe-function) should return an unquoted procedure, similar to </div>
<div><br></div><div>(define (select-random-safe-function) ;without object-name</div><div>   (list-ref safe-function-set (random (length safe-function-set)))) </div>
<div>>(select-random-safe-function)</div><div>#<procedure:$+></div><div><br></div><div>preferably without <span style="color:rgb(35,35,35);font-family:Arial;font-size:13px;text-decoration:line-through;letter-spacing:0px">#<procedure: </span><span style="color:rgb(35,35,35);font-family:Arial;font-size:13px;letter-spacing:0px">$+</span><span style="color:rgb(35,35,35);font-family:Arial;font-size:13px;text-decoration:line-through;letter-spacing:0px"> ></span></div>
<div><span style="font-family:arial,sans-serif;font-size:13px"><br>

</span></div><div><div><b>so my problem is that I want the output to display <br></b></div><div><b>$+</b></div><div><b>instead of</b></div><div><b>#<procedure:$+></b><br></div></div><div><br></div><div>2. The reason the procedure needs to be unquoted is because </div>


<div>(procedure-arity (select-random-safe-function)) will produce an error if the function is quoted.</div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">Later on I use both </font>(procedure-arity (select-random-safe-function)) and (select-random-safe-function) in a main function:</div>
</div>

<div><br></div><div>;;Generate Initial Population</div><div>;population size is a positive integer and the auxiliary-function, generate-chromsome generates a tree using functions from safe-function-set and terms (numbers and symbols) from a term-set</div>


<div><br></div><div><div>(define (generate-initial-population population-size) </div><div>  (cond [(zero? population-size) empty]</div><div>           [else (cons (generate-chromosome initial-chromosome-depth) ;initial-chromosome-depth is 3</div>


<div>                            (generate-initial-population (sub1 population-size)))]))</div></div><div><br></div><div>> (generate-initial-population 1) </div><div><div>'((#<procedure:$/> (#<procedure:$-> x .234) (#<procedure:$+> .576 x)))<br>


</div></div><div><br></div><div>this gets me my desired result, but is hard for me to read especially as the depth and population size grows (typically I will be working with populations ranging in the hundreds or thousands and each chromosome/tree's depth could easily exceed 10) so<b> it would be more convenient here if my solution displays <br>
</b></div><div><b><br></b></div><div><b>'(($/ ($- x .234) ($+ .576 x))) <br>

</b></div><div><b><br></b></div><div><b>instead of</b></div><div><div><b><br></b></div><div><b>'((#<procedure:$/> (#<procedure:$-> x </b><b>.234</b><b>) (#<procedure:$+> </b><b>.576</b><b> x)))</b></div>
</div><div><br></div><div class="gmail_extra"><b>and I am not sure how I would go about doing this?</b><div class="gmail_quote"><br></div><div class="gmail_quote">On Sat, Jan 4, 2014 at 8:45 AM, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>If you really just want the name, use object-name: </div>
<div><br>
</div><div><div>(define (select-random-safe-function)</div><div>  (object-name </div><div>   (list-ref safe-function-set (random (length safe-function-set)))))</div></div><div><br></div><div><div>Welcome to DrRacket, version 6.0.0.1--2013-12-29(bbb0c5f6/d) [3m].</div>

<div>Language: racket.</div><div>> (select-random-safe-function)</div><div>'$*</div><div><br></div></div><div><div><div><br></div><div><br></div><br><div><div>On Jan 4, 2014, at 2:48 AM, Rian Shams wrote:</div>
<br><blockquote type="cite"><div dir="ltr">If I do this I get the results I need for <span style="font-family:arial,sans-serif;font-size:13px">(procedure-arity (select-random-safe-function)), but when I call </span><div>
<span style="font-family:arial,sans-serif;font-size:13px">(safe-function-set) or (select-random-safe-function) or any other function that returns a safe function I get for example:</span><span style="font-family:arial,sans-serif;font-size:13px"><br>


</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">>(safe-function-set)</span></div><div>'(#<procedure:$+> #<procedure:$-> #<procedure:$*> #<procedure:$/>)<span style="font-family:arial,sans-serif;font-size:13px"><br>


</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">></font><span style="font-family:arial,sans-serif;font-size:13px">(select-random-safe-function)</span><span style="font-family:arial,sans-serif;font-size:13px"> </span></div>


<div>#<procedure:$-><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><br></div><div>whereas when it was defined using quote as opposed to list I get:</div><div><br></div><div><div><span style="font-family:arial,sans-serif;font-size:13px">>(safe-function-set)</span></div>


<div>'($+ $- $* $/)<span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">></font><span style="font-family:arial,sans-serif;font-size:13px">(select-random-safe-function)</span><span style="font-family:arial,sans-serif;font-size:13px"> </span></div>


<div>'$-</div></div><div><br></div><div><b>How would I get rid of this <u>#<procedure:</u>$-<u>></u> part for readability, keeping only the $- part which is the actual function I defined, while still keeping it a function/procedure that gives me the function arity using <span style="font-family:arial,sans-serif;font-size:13px">(procedure-arity (select-random-safe-function))?</span></b></div>


<div><b><span style="font-family:arial,sans-serif;font-size:13px"><br></span></b></div><div><span style="font-family:arial,sans-serif;font-size:13px">Best,</span></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">


On Fri, Jan 3, 2014 at 5:31 PM, Sam Tobin-Hochstadt <span dir="ltr"><<a href="mailto:samth@cs.indiana.edu" target="_blank">samth@cs.indiana.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


Try replacing `safe-function-set` with:<br>
<br>
(define safe-function-set (list $+ $- $* $/))<br>
<br>
Sam<br>
<div><div><br>
On Fri, Jan 3, 2014 at 5:28 PM, Rian Shams <<a href="mailto:rian.shams@gmail.com" target="_blank">rian.shams@gmail.com</a>> wrote:<br>
> Hello,<br>
><br>
> I am working with functions that I have defined to only take 1 or 2 operands<br>
> (called safe-functions) for the implementation of a genetic program.<br>
><br>
> (define ($+ augend addend) ;operation is addition<br>
>   (+ augend addend)) ; the result is the sum<br>
> (define ($- minuend subtrahend) ;operation is subtraction<br>
>   (- minuend subtrahend)) ;result is difference<br>
> (define ($* multiplicand multiplier) ;operation is multiplication<br>
>   (* multiplicand multiplier)) ;result is product<br>
> (define ($/ dividend divisor) ;operation is division<br>
>   (/ dividend divisor)) ;result is quotient<br>
><br>
> (define (infinity? x) (or (eq? x +Inf.0) (eq? x -Inf.0)))<br>
> (define ($sin x) (if (infinity? x) (* (sgn x) +Inf.0) (sin x)))<br>
> (define ($cos x) (if (infinity? x) (* (sgn x) +Inf.0) (cos x)))<br>
><br>
> (define safe-function-set<br>
>   '($+<br>
>     $-<br>
>     $*<br>
>     $/<br>
>     ;$sin<br>
>     ;$cos))<br>
><br>
> (define (select-random-safe-function)<br>
>   (list-ref safe-function-set (random (length safe-function-set))))<br>
><br>
> I would like to use procedure-arity (or a similar function) to determine the<br>
> arity of a randomly selected safe function but I get this error:<br>
><br>
>>(procedure-arity (select-random-safe-function))<br>
> error: procedure-arity: contract violation<br>
>   expected: procedure?<br>
>   given: '$+<br>
><br>
> I think the problem is that the safe-functions are passed to procedure-arity<br>
> quoted. Is there a way I can unquote the functions, or adjust<br>
> procedure-arity to make (procedure-arity (select-random-safe-function))<br>
> work?<br>
><br>
> Thanks,<br>
> --<br>
> Rian Shams<br>
><br>
</div></div>> ____________________<br>
>   Racket Users list:<br>
>   <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Rian Shams
</div>
____________________<br>  Racket Users list:<br>  <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br></blockquote></div><br></div></div></div></blockquote></div><br><br clear="all">

<div><br></div>-- <br>Rian Shams
</div></div>
</blockquote></div><br></body></html>