[racket] racket newbie question on quoting and the built in procedure-arity function

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Jan 4 08:45:51 EST 2014

If you really just want the name, use object-name: 

(define (select-random-safe-function)
  (object-name 
   (list-ref safe-function-set (random (length safe-function-set)))))

Welcome to DrRacket, version 6.0.0.1--2013-12-29(bbb0c5f6/d) [3m].
Language: racket.
> (select-random-safe-function)
'$*




On Jan 4, 2014, at 2:48 AM, Rian Shams wrote:

> If I do this I get the results I need for (procedure-arity (select-random-safe-function)), but when I call 
> (safe-function-set) or (select-random-safe-function) or any other function that returns a safe function I get for example:
> 
> >(safe-function-set)
> '(#<procedure:$+> #<procedure:$-> #<procedure:$*> #<procedure:$/>)
> 
> >(select-random-safe-function) 
> #<procedure:$->
> 
> whereas when it was defined using quote as opposed to list I get:
> 
> >(safe-function-set)
> '($+ $- $* $/)
> 
> >(select-random-safe-function) 
> '$-
> 
> How would I get rid of this #<procedure:$-> 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 (procedure-arity (select-random-safe-function))?
> 
> Best,
> 
> 
> On Fri, Jan 3, 2014 at 5:31 PM, Sam Tobin-Hochstadt <samth at cs.indiana.edu> wrote:
> Try replacing `safe-function-set` with:
> 
> (define safe-function-set (list $+ $- $* $/))
> 
> Sam
> 
> On Fri, Jan 3, 2014 at 5:28 PM, Rian Shams <rian.shams at gmail.com> wrote:
> > Hello,
> >
> > I am working with functions that I have defined to only take 1 or 2 operands
> > (called safe-functions) for the implementation of a genetic program.
> >
> > (define ($+ augend addend) ;operation is addition
> >   (+ augend addend)) ; the result is the sum
> > (define ($- minuend subtrahend) ;operation is subtraction
> >   (- minuend subtrahend)) ;result is difference
> > (define ($* multiplicand multiplier) ;operation is multiplication
> >   (* multiplicand multiplier)) ;result is product
> > (define ($/ dividend divisor) ;operation is division
> >   (/ dividend divisor)) ;result is quotient
> >
> > (define (infinity? x) (or (eq? x +Inf.0) (eq? x -Inf.0)))
> > (define ($sin x) (if (infinity? x) (* (sgn x) +Inf.0) (sin x)))
> > (define ($cos x) (if (infinity? x) (* (sgn x) +Inf.0) (cos x)))
> >
> > (define safe-function-set
> >   '($+
> >     $-
> >     $*
> >     $/
> >     ;$sin
> >     ;$cos))
> >
> > (define (select-random-safe-function)
> >   (list-ref safe-function-set (random (length safe-function-set))))
> >
> > I would like to use procedure-arity (or a similar function) to determine the
> > arity of a randomly selected safe function but I get this error:
> >
> >>(procedure-arity (select-random-safe-function))
> > error: procedure-arity: contract violation
> >   expected: procedure?
> >   given: '$+
> >
> > I think the problem is that the safe-functions are passed to procedure-arity
> > quoted. Is there a way I can unquote the functions, or adjust
> > procedure-arity to make (procedure-arity (select-random-safe-function))
> > work?
> >
> > Thanks,
> > --
> > Rian Shams
> >
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
> >
> 
> 
> 
> -- 
> Rian Shams
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140104/2cfee404/attachment-0001.html>

Posted on the users mailing list.