Ah, I forgot about for/sum. This version is probably clearer:
(struct polynomial (coeffs)
#:transparent
#:property prop:procedure
(lambda (poly num)
(for/sum ([x (length (polynomial-coeffs poly))]
[c (polynomial-coeffs poly)])
(* c (expt num x)))))
---
Justin Slepak
PhD student, Computer Science dept.
----- Original Message -----
From: Matthias Felleisen <[email protected]>
To: Justin R. Slepak <[email protected]>
Cc: [email protected]
Sent: Mon, 15 Oct 2012 10:02:17 -0400 (EDT)
Subject: Re: [racket] Function composition in Racket
Do you want to try for/sum here?
On Oct 14, 2012, at 10:28 PM, Justin R. Slepak wrote:
> To use prop:procedure, just give a function which will handle the application
> of the structure to some arguments. The define-values is only there because
> the for/fold has two accumulators (sum and x) and will therefore return two
> values (the values of those accumulators). This means its context has to
> expect two values, even though we don't really care about the second value.
> The x* is just a name.
>
> ---
> Justin Slepak
> PhD student, Computer Science dept.
>
> ----- Original Message -----
> From: Gregory Woodhouse <[email protected]>
> To: Justin R. Slepak <[email protected]>
> Sent: Sun, 14 Oct 2012 22:07:59 -0400 (EDT)
> Subject: Re: [racket] Function composition in Racket
>
> Thanks! This does what I want. To tell you the truth, I've shied away from
> prop:procedure (probably more due to my own confusion than anything else!).
> The define-values here seems a bit mysterious, but I assume the point is to
> support the recursive polynomial evaluation function? Is x* a special syntax
> here or just a name. I see sum in both for/fold and values, but the x* in
> define-values is mysterious.
>
> On Oct 14, 2012, at 4:57 PM, "Justin R. Slepak" <[email protected]> wrote:
>
>> Instead of trying to peek inside a lambda, you could implement polynomials
>> as (transparent) structs and use prop:procedure to allow them to be applied
>> to numbers:
>>
>> (struct polynomial (coeffs)
>> #:transparent
>> #:property prop:procedure
>> (lambda (poly num)
>> (define-values (result x*)
>> (for/fold ([sum 0]
>> [x 1])
>> ([c (polynomial-coeffs poly)])
>> (values (+ sum (* c x))
>> (* x num))))
>> result))
>>
>> This would let you implement functions that crunch on polynomials. As for
>> using existing operators' names, maybe generics can get you that?
>>
>> ---
>> Justin Slepak
>> PhD student, Computer Science dept.
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
____________________
Racket Users list:
http://lists.racket-lang.org/users