[racket] Newbie question on abstraction and the built in random function

From: Rian Shams (rian.shams at gmail.com)
Date: Thu Dec 19 21:42:51 EST 2013

Hello All,

I am new to Racket and just started building an evolutionary computing
module to supplement my research and learn more about Racket.

When it comes to representing a chromosome, there are many encoding schemes
one can choose from. To start I would like to build a function that lets
the user choose the encoding scheme they would like by being able to
directly insert it into the function call. So far I have created this:

(define (generate-chromosome encoding-scheme number-of-genes)
  (cond  [(zero? number-of-genes) empty]
            [else (cons encoding-scheme
                             (generate-chromosome encoding-scheme (sub1
number-of-genes)))]))

Two popular encoding schemes are the binary encoding scheme and the real
value encoding scheme:

(define binary-encoding-scheme
  (random 2))
(define value-encoding-scheme
  (random))

So then calling this function using the binary encoding scheme:

(generate-chromosome binary-encoding-scheme 10)

gives either a list of only ten 1's or a list of only ten 0's. Random only
works once and whatever the first random value is stays and doesn't change.

*How would I make random re-evaluate each time given that I want it to be
included in the definition of an encoding scheme?*

As an alternate, the function below works when I directly include (random
2) in for a value in the encoding scheme, but I prefer the structure of the
above function.

(define (generate-chromosome-2 number-of-genes)
  (cond [(zero? number-of-genes) empty]
           [else (cons (random 2)         ;binary-encoding-scheme
                            (generate-chromosome-2 (sub1
number-of-genes)))]))

So the function call
(generate-chromosome-2 10) gives the desired result, but doesn't fit the
needed structure.

Any help would be much appreciated.

Best Regards,
-- 
Rian Shams
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131219/a9cc94d3/attachment.html>

Posted on the users mailing list.