[racket] prop:procedure and arity

From: Alexander D. Knauth (alexander at knauth.org)
Date: Mon Feb 9 19:57:05 EST 2015

On Feb 9, 2015, at 7:46 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:

> At Mon, 9 Feb 2015 19:36:56 -0500, "Alexander D. Knauth" wrote:
>> Say I want a struct that will act as a procedure.
>> For a given instance of that struct, I want the procedure to have a given 
>> arity, but I don’t want the procedure to be a field in the struct.
>> Is there a way to do this without making the procedure a field?
> 
> I think there's no way to do that, assuming that you want a single
> constructor that answers #t to `struct-constructor-procedure?`.
> 
> If you get to substitute a function for the constructor, then you could
> make that function use `procedure-reduce-arity` on each raw instance,
> or you could generate a structure subtype for each arity and
> instantiate a subtype based on field values.

That won’t work because the result of procedure-reduce-arity won’t be an instance of my struct.
For example:
#lang racket
(define (make-my-f lst)
  (procedure-reduce-arity (my-f lst) (length lst)))
(struct my-f (lst)
  #:transparent
  #:property prop:procedure
  (lambda (this . args)
    (define lst (my-f-lst this))
    (unless (= (length args) (length lst))
      (error 'my-f "wrong number of arguments, expected ~v, recieved ~v" (length lst) args))
    (for/sum ([x (in-list args)]
              [a (in-list lst)])
      (* a x))))
This does’t work because the procedure that make-my-f returns won’t be an instance of my-f.




Posted on the users mailing list.