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

From: Richard Cleis (rcleis at me.com)
Date: Fri Dec 20 01:52:29 EST 2013

You might want your encoding-scheme to be a function, 
and you want to evaluate that function inside generate-chromosome:

(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)))]))

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

(generate-chromosome binary-encoding-scheme 10)
(generate-chromosome value-encoding-scheme 10)

(0 1 1 1 1 1 0 1 1 0)
(0.17586764776624525
 0.13437314563189037
 0.585523111696543
 0.6533766686688987
 0.09071850144989983
 0.8804515074319006
 0.6998388642832832
 0.599233672637633
 0.5238661544779689
 0.72734166222789)
> 


rac



On Dec 19, 2013, at 7:42 PM, Rian Shams wrote:

> 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
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131219/7f9ddc37/attachment.html>

Posted on the users mailing list.