jeudi 14 janvier 2016

Save all results generated from calling the same function with different arity.

I have multiple modules that call a function: my-random the function takes either 0 or 1 arguments. What I want to do is write the results to all calls to that function and save it in a file, then re-run the program but instead provide it with that file in order to use these random numbers.

This is what I have for saving the output of the function:

  (define random-port (open-output-file "random-numbers.rktl" #:exists 'replace))

(define (my-random [x #f])
    (define y (if x (random0 x) (random0)))
    (displayln y random-port)
    y)

This doesn't save all the numbers though because I'm supposed to close the port after finishing all the calls, but I don't want to close it at a specific point. Is there anyway around this?

Secondly, that function is how we then get back the random numbers and re-use them. The issue is that I would get an arity mismatch because my-random has 0 or 1 arguments, and again, can this be fixed?

  (require racket/generator)

  (define my-random
    (generator
     (_)
     (for ([x (file->list "random-numbers.rktl")])
       (yield x))))

Thank you so much!




Aucun commentaire:

Enregistrer un commentaire