[racket] Plot: #:samples parameter of function---is this what was meant?

From: Deren Dohoda (deren.dohoda at gmail.com)
Date: Wed Apr 18 15:27:11 EDT 2012

This is very counter-intuitive to me:

#lang racket
(require plot)

(plot (list (axes)
            (function (λ(x) (* x x)) #:samples 4 -2 0) ; 2 samples?!
            (function (λ(x) (* x x)) #:samples 8 0 2))) ; 4 samples?!

It is really surprising to suppose that the number of samples isn't
specifying the number of times to sample this function *in the range
given*. Was this intentional? I notice the plot procedure itself has
no #:samples parameter, which is where I would have expected this
behavior to make sense (for example, "sample all functions in the
renderer-tree this way"). But within the call to "function" I'd have
guessed that the samples parameter applies to the function with the
specified range. It doesn't.

This manages the issue easily enough:

(define (my-function f #:samples (s 500) min max)
  (define (range->samples min max desired-samples)
    (* (- max min) desired-samples))
  (function f #:samples (range->samples min max s) min max))

(plot (list (axes)
            (my-function (λ(x) (* x x)) #:samples 4 -2 0) ; no really, 4 samples
            (my-function (λ(x) (* x x)) #:samples 8 0 2))) ; " " 8

I was wondering if there were some reasoning behind the decision to
have the implementation the way it is that might help me remember
this, or whether this parameter should be explained, or whether I'm
just crazy and obviously it should have the behavior it does. :)


Posted on the users mailing list.