Re: [racket-users] Creating a list of random things

2016-10-26 Thread Gustavo Massaccesi
An additional example, I hope it's useful.

I think it's easy to explain if we replace (random) with a new
function (get-counter!) that instead of generating a random number, it
provides an increasing sequence.

#lang racket
(define secret-counter 0)
(define (get-counter!)
  (displayln "secret-counter increased")
  (set! secret-counter (add1 secret-counter))
  secret-counter)

(make-list 5 (get-counter!))  ;==> '(1 1 1 1 1)
(build-list 5 (lambda (ignored) (get-counter!)))  ;==> '(2 3 4 5 6)
(make-list 5 (get-counter!))  ;==> '(7 7 7 7 7)

In the first example, make-list is a function, so all the arguments
are evaluated before running the function. The result of
(get-counter!) is 1 so
   (make-list 5 (get-counter!))
is equivalent to
   (make-list 5 1)
so you get
   '(1 1 1 1 1)

In the second example, build-list is also a function, but the second
argument is an anonymous function instead of a simple value. So
build-list calls the anonymous function 5 time (each time with a
different argument that is ignored in this example).

In the third example, we use make-list again so (get-counter!) is
called only once again, but this time the value is 7 so the result is
the result of (make-list 5 7)

Gustavo



On Tue, Oct 25, 2016 at 11:47 PM, Ben Greenman
 wrote:
> Or:
>
> (build-list n (lambda (dont-care) (make-random-string)))
>
> On Tue, Oct 25, 2016 at 10:42 PM, Robby Findler
>  wrote:
>>
>> I would probably write that function like this:
>>
>> #lang racket
>>
>> (provide
>>  (contract-out
>>   [make-a-string (-> non-empty-string? string?)]))
>>
>> (define (make-a-string candidates)
>>   (apply
>>string
>>(for/list ([i (in-range (random 100))])
>>  (string-ref candidates (random (string-length candidates))
>>
>> (module+ test
>>   (require rackunit)
>>   (check-true (string? (make-a-string
>> "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
>>
>>
>> hth,
>> Robby
>>
>>
>> On Tue, Oct 25, 2016 at 9:39 PM,   wrote:
>> > Hi,
>> >
>> > I want to create a list of random strings.
>> > For that I have created a function, which returns a string
>> > of random length and random contents.
>> >
>> > Then I used
>> > make-list n (make-random-string)
>> >
>> > to create that listand get back a list filled with n
>> > identical strings.
>> >
>> > h...
>> >
>> > make-list calls make-random-string ones and the rest
>> > is repetition.
>> >
>> > If I think to have understood correctly for-loops and such are
>> > somehow of "imperative, non-functional old-school programming style"
>> > and map/apply and friends are the tools of the real racket programmer
>> > ;) this lead to a more general "problem" ("problem" only for a newbie
>> > like me may be)...:
>> >
>> > Since make-list calls its list-argument only once, the above does not
>> > work in the sense it is wanted, so make-list creates only lists of
>> > the same thing.
>> >
>> > Therefore to prevent "for" and friends, I have to call make-list
>> > with the empty list as list-argument and then map the
>> > make-random-string to each of them, which ensures that
>> > make-random-string is not called once.
>> > That feels ugly.
>> >
>> > What is a good "racket-ish" of "racket-y" way (nothing negative
>> > meant...just a play with words by someone, who is no native
>> > speaker...;) to do something n times without old-school
>> > for-and-friends-loops.
>> >
>> > Here is the code:
>> >
>> > #lang racket
>> > (require racket/random)
>> > (require racket/string)
>> >
>> > (define charseq "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
>> > (define minchars  7)
>> > (define maxchars 15)
>> > (define numlists 6)
>> >
>> > (define (make-random-string seq min max)
>> >   ( let ((lenstr (random min max)))
>> > ( apply string (random-sample seq lenstr
>> >
>> > (define (make-list-of-random-strings cnt seq min max )
>> >   (make-list cnt (make-random-string seq min max )))
>> >
>> > (define (make-list-of-lists numsublists)
>> >   ( make-list numsublists (make-list-of-random-strings numlists charseq
>> > minchars maxchars)))
>> >
>> > ( define (main)
>> >  (make-list-of-lists 20))
>> >
>> > (main)
>> >
>> > Any improvement is welcome!
>> >
>> > Cheers,
>> > Meino
>> >
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Racket Users" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an email to racket-users+unsubscr...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You 

Re: [racket-users] Creating a list of random things

2016-10-25 Thread Ben Greenman
Or:

(build-list n (lambda (dont-care) (make-random-string)))

On Tue, Oct 25, 2016 at 10:42 PM, Robby Findler  wrote:

> I would probably write that function like this:
>
> #lang racket
>
> (provide
>  (contract-out
>   [make-a-string (-> non-empty-string? string?)]))
>
> (define (make-a-string candidates)
>   (apply
>string
>(for/list ([i (in-range (random 100))])
>  (string-ref candidates (random (string-length candidates))
>
> (module+ test
>   (require rackunit)
>   (check-true (string? (make-a-string "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123
> 456789"
>
>
> hth,
> Robby
>
>
> On Tue, Oct 25, 2016 at 9:39 PM,   wrote:
> > Hi,
> >
> > I want to create a list of random strings.
> > For that I have created a function, which returns a string
> > of random length and random contents.
> >
> > Then I used
> > make-list n (make-random-string)
> >
> > to create that listand get back a list filled with n
> > identical strings.
> >
> > h...
> >
> > make-list calls make-random-string ones and the rest
> > is repetition.
> >
> > If I think to have understood correctly for-loops and such are
> > somehow of "imperative, non-functional old-school programming style"
> > and map/apply and friends are the tools of the real racket programmer
> > ;) this lead to a more general "problem" ("problem" only for a newbie
> > like me may be)...:
> >
> > Since make-list calls its list-argument only once, the above does not
> > work in the sense it is wanted, so make-list creates only lists of
> > the same thing.
> >
> > Therefore to prevent "for" and friends, I have to call make-list
> > with the empty list as list-argument and then map the
> > make-random-string to each of them, which ensures that
> > make-random-string is not called once.
> > That feels ugly.
> >
> > What is a good "racket-ish" of "racket-y" way (nothing negative
> > meant...just a play with words by someone, who is no native
> > speaker...;) to do something n times without old-school
> > for-and-friends-loops.
> >
> > Here is the code:
> >
> > #lang racket
> > (require racket/random)
> > (require racket/string)
> >
> > (define charseq "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
> > (define minchars  7)
> > (define maxchars 15)
> > (define numlists 6)
> >
> > (define (make-random-string seq min max)
> >   ( let ((lenstr (random min max)))
> > ( apply string (random-sample seq lenstr
> >
> > (define (make-list-of-random-strings cnt seq min max )
> >   (make-list cnt (make-random-string seq min max )))
> >
> > (define (make-list-of-lists numsublists)
> >   ( make-list numsublists (make-list-of-random-strings numlists charseq
> minchars maxchars)))
> >
> > ( define (main)
> >  (make-list-of-lists 20))
> >
> > (main)
> >
> > Any improvement is welcome!
> >
> > Cheers,
> > Meino
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to racket-users+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Creating a list of random things

2016-10-25 Thread Robby Findler
I would probably write that function like this:

#lang racket

(provide
 (contract-out
  [make-a-string (-> non-empty-string? string?)]))

(define (make-a-string candidates)
  (apply
   string
   (for/list ([i (in-range (random 100))])
 (string-ref candidates (random (string-length candidates))

(module+ test
  (require rackunit)
  (check-true (string? (make-a-string "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"


hth,
Robby


On Tue, Oct 25, 2016 at 9:39 PM,   wrote:
> Hi,
>
> I want to create a list of random strings.
> For that I have created a function, which returns a string
> of random length and random contents.
>
> Then I used
> make-list n (make-random-string)
>
> to create that listand get back a list filled with n
> identical strings.
>
> h...
>
> make-list calls make-random-string ones and the rest
> is repetition.
>
> If I think to have understood correctly for-loops and such are
> somehow of "imperative, non-functional old-school programming style"
> and map/apply and friends are the tools of the real racket programmer
> ;) this lead to a more general "problem" ("problem" only for a newbie
> like me may be)...:
>
> Since make-list calls its list-argument only once, the above does not
> work in the sense it is wanted, so make-list creates only lists of
> the same thing.
>
> Therefore to prevent "for" and friends, I have to call make-list
> with the empty list as list-argument and then map the
> make-random-string to each of them, which ensures that
> make-random-string is not called once.
> That feels ugly.
>
> What is a good "racket-ish" of "racket-y" way (nothing negative
> meant...just a play with words by someone, who is no native
> speaker...;) to do something n times without old-school
> for-and-friends-loops.
>
> Here is the code:
>
> #lang racket
> (require racket/random)
> (require racket/string)
>
> (define charseq "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
> (define minchars  7)
> (define maxchars 15)
> (define numlists 6)
>
> (define (make-random-string seq min max)
>   ( let ((lenstr (random min max)))
> ( apply string (random-sample seq lenstr
>
> (define (make-list-of-random-strings cnt seq min max )
>   (make-list cnt (make-random-string seq min max )))
>
> (define (make-list-of-lists numsublists)
>   ( make-list numsublists (make-list-of-random-strings numlists charseq 
> minchars maxchars)))
>
> ( define (main)
>  (make-list-of-lists 20))
>
> (main)
>
> Any improvement is welcome!
>
> Cheers,
> Meino
>
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Creating a list of random things

2016-10-25 Thread Meino . Cramer
Hi,

I want to create a list of random strings.
For that I have created a function, which returns a string
of random length and random contents.

Then I used 
make-list n (make-random-string)

to create that listand get back a list filled with n
identical strings.

h...

make-list calls make-random-string ones and the rest
is repetition.

If I think to have understood correctly for-loops and such are
somehow of "imperative, non-functional old-school programming style"
and map/apply and friends are the tools of the real racket programmer
;) this lead to a more general "problem" ("problem" only for a newbie
like me may be)...:

Since make-list calls its list-argument only once, the above does not
work in the sense it is wanted, so make-list creates only lists of
the same thing.

Therefore to prevent "for" and friends, I have to call make-list
with the empty list as list-argument and then map the 
make-random-string to each of them, which ensures that
make-random-string is not called once.
That feels ugly.

What is a good "racket-ish" of "racket-y" way (nothing negative
meant...just a play with words by someone, who is no native 
speaker...;) to do something n times without old-school
for-and-friends-loops.

Here is the code:

#lang racket
(require racket/random)
(require racket/string)

(define charseq "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
(define minchars  7)
(define maxchars 15)
(define numlists 6)

(define (make-random-string seq min max)
  ( let ((lenstr (random min max)))
( apply string (random-sample seq lenstr

(define (make-list-of-random-strings cnt seq min max )
  (make-list cnt (make-random-string seq min max )))

(define (make-list-of-lists numsublists)
  ( make-list numsublists (make-list-of-random-strings numlists charseq 
minchars maxchars)))

( define (main)
 (make-list-of-lists 20))

(main)

Any improvement is welcome!

Cheers,
Meino



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.