egallesio / STklos

STklos Scheme
http://stklos.net
GNU General Public License v2.0
67 stars 17 forks source link

Add one more example #634

Closed jpellegrini closed 2 weeks ago

jpellegrini commented 1 month ago

dice.stk is a very simple example of scheme program that asks for a number N, then runs two dice N times, and finally prints a histogram of the distribution of outcomes.

Concepts illustrated:

It's a very basic example of a Scheme program, but I guess it is interesting for someone beginning to use SRFIs and libraries. :)

egallesio commented 2 weeks ago

Thanks for this example. I have merged your PR.

I see that you have tried to be very standard in your writing (it seems to be aimed at students, am I right?). I think it would be interesting to add a possibility to pass the number of experiments from the command line (and use read if it is not the case). We could replace the run function by (probably the 4 last lines should be in a separate procedure)

(define (main argv)
  ;; This is the main procedure. If we have arguments on the command line, we
  ;; use the first one as the number of iterations. Otherwise, we ask for this
  ;; number. Once we have that number we run the experiment and plot the histogram
  (let ((R (if (> (length argv) 1)
               ;; We have at least an argument and argv contains all the words of
               ;; the command line. If we entered at  the prompt:
               ;;     $ dice 10_000
               ;; argv will be ("dice" "10_000" ...).
               ;; We use read-from-string her to convert the argument to a number
               (read-from-string (list-ref argv 1))
               ;; No argument => ask interactively to the user
               (begin
                 (display "How many times should I throw the two dices? ")
                 (read)))))

    ;; R is the number of iterations we have to run
    (newline)
    (display "Ok, here is a histogram of the distribution:")
    (newline)
    (plot-distribution (make-histogram (run-experiment R)))))

This is a bit more complicate, and perhaps its doesn't fulfill your initial goal with this example. What do you think?

jpellegrini commented 2 weeks ago

Hi @egallesio ! No, I don't think it's too complicated. Beginners like when the program looks like a "real world program". I like your version!

egallesio commented 1 week ago

So, I'll modify it accordingly.