Re: [racket-users] named let ...

2020-11-26 Thread George Neuner


On 11/26/2020 11:22 PM, Tim Meehan wrote:
I was trying to turn one of Knuth's random number sampling without 
replacement algorithms. It seems to be working, but when it comes time 
to return my prize ... nothing :(


I thought that since I was returning from inside of the named let, 
that I'd get back my collection of numbers sampled without replacement.


;; algorithm taken from:
;; 
https://stackoverflow.com/questions/311703/algorithm-for-sampling-without-replacement 


(define (sample-without-replacement n N)
  (let loop ([num-seen 0]
   [num-stored 0]
   [result '()])
    (let ([u (random)])
(display (format "~a\n" result))
(unless (>= num-stored n)
(if (>= (* u (- N num-seen)) (- n num-stored))
  (loop (add1 num-seen) num-stored result)
  (loop (add1 num-seen) (add1 num-stored) (cons num-seen result
result)))


How to explain this ...

The problem is that 'unless' does not end the function - the calls to 
'loop' are *not* in tail position, so although you are recursing, you 
are not tail-recursing.  Your code doesn't exit at the bottom but rather 
unwinds the call stack, returning from 'loop' and and evaluating 
'result' every time.  At the top level, 'result' is the empty list 
(passed as the argument) and that is what is being returned.


Contrast with:
  (if (>= num-stored n)
    result
    (if (>= (* u (- N num-seen)) (- n num-stored))
    (loop (add1 num-seen) num-stored result)
    (loop (add1 num-seen) (add1 num-stored) (cons num-seen 
result


When you have this kind of multiple test logic, 'cond' is your friend:
  (cond
    ((>= num-stored n)
 result)
    ((>= (* u (- N num-seen)) (- n num-stored))
 (loop (add1 num-seen) num-stored result))
    (else
 (loop (add1 num-seen) (add1 num-stored) (cons num-seen result

Hope this helps,
George

--
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/4e21e51e-0d8e-8b76-8422-7b88d3972415%40comcast.net.


[racket-users] named let ...

2020-11-26 Thread Tim Meehan
I was trying to turn one of Knuth's random number sampling without
replacement algorithms. It seems to be working, but when it comes time to
return my prize ... nothing :(

I thought that since I was returning from inside of the named let, that I'd
get back my collection of numbers sampled without replacement.

;; algorithm taken from:
;;
https://stackoverflow.com/questions/311703/algorithm-for-sampling-without-replacement
(define (sample-without-replacement n N)
  (let loop ([num-seen 0]
 [num-stored 0]
 [result '()])
(let ([u (random)])
  (display (format "~a\n" result))
  (unless (>= num-stored n)
(if (>= (* u (- N num-seen)) (- n num-stored))
(loop (add1 num-seen) num-stored result)
(loop (add1 num-seen) (add1 num-stored) (cons num-seen
result
  result)))

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CACgrOxLK7mx5svyC34uKm4XRcXVBBvP9hq83M7WhBStNRA%2Bpaw%40mail.gmail.com.


Re: [racket-users] Rosette: can it help me

2020-11-26 Thread Bertrand Augereau
Thank you very much for the fast and precise answer, I'll study this !

Le jeu. 26 nov. 2020 à 22:24, Sorawee Porncharoenwase
 a écrit :
>
> You are doing a recursion on a symbolic value, which is a common pitfall in 
> Rosette.
>
> See slide 81 and subsequent slides for more details. Slide 87 shows a 
> solution to this problem.
>
>
> On Thu, Nov 26, 2020 at 1:12 PM Bertrand Augereau 
>  wrote:
>>
>> Hello everybody,
>>
>> not sure if this is the good place to ask about this but I couldn't
>> find a good location so I'm asking for help here, after all it's
>> strongly connected to Racket :)
>>
>> I'm trying to reverse-engineer some specific piece of hardware with
>> the help of Rosette, to discover if it can help or not. In the long
>> run I want it to help me find magic numbers for a specific routine to
>> match captures of the hardware output (can't say more about this
>> though).
>>
>> After dumbing down (a lot) my first tries, I'm having issues with this
>> code running endlessly:
>> #lang rosette/safe
>>
>> (define (zero-a x) 0)
>> (define (zero-b x)
>>   (if (= x 0) 0 (zero-b (- x 1
>>
>> (define-symbolic x integer?)
>> (assert (>= x 0))
>> (assert (<= x 10))
>>
>> (verify (assert (= (zero-a x) (zero-b x
>>
>> I don't have a strong theoretical understanding of what Rosette can
>> and cannot do, but I thought that by restraining the range with the
>> asserts, at least I'd get a brute force search of the solution space,
>> which should be fast.
>> Does it make sense ? Can somebody explain me where is my misunderstanding ?
>>
>> Thank you folks,
>> Bertrand
>>
>> --
>> 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.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/CAHV%3D05otPXjbrEaHWvJF-SeDSxz2-Mjq3p%3DSHR3HvuJRAJJ3-A%40mail.gmail.com.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAHV%3D05rhby63VMWfP0RV-BL%3DsYSQAv%3DdoUfj%3DChC4fYjW16dkA%40mail.gmail.com.


Re: [racket-users] Rosette: can it help me

2020-11-26 Thread Sorawee Porncharoenwase
You are doing a recursion on a symbolic value, which is a common pitfall in
Rosette.

See slide 81
 and
subsequent slides for more details. Slide 87 shows a solution to this
problem.

On Thu, Nov 26, 2020 at 1:12 PM Bertrand Augereau <
bertrand.auger...@gmail.com> wrote:

> Hello everybody,
>
> not sure if this is the good place to ask about this but I couldn't
> find a good location so I'm asking for help here, after all it's
> strongly connected to Racket :)
>
> I'm trying to reverse-engineer some specific piece of hardware with
> the help of Rosette, to discover if it can help or not. In the long
> run I want it to help me find magic numbers for a specific routine to
> match captures of the hardware output (can't say more about this
> though).
>
> After dumbing down (a lot) my first tries, I'm having issues with this
> code running endlessly:
> #lang rosette/safe
>
> (define (zero-a x) 0)
> (define (zero-b x)
>   (if (= x 0) 0 (zero-b (- x 1
>
> (define-symbolic x integer?)
> (assert (>= x 0))
> (assert (<= x 10))
>
> (verify (assert (= (zero-a x) (zero-b x
>
> I don't have a strong theoretical understanding of what Rosette can
> and cannot do, but I thought that by restraining the range with the
> asserts, at least I'd get a brute force search of the solution space,
> which should be fast.
> Does it make sense ? Can somebody explain me where is my misunderstanding ?
>
> Thank you folks,
> Bertrand
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CAHV%3D05otPXjbrEaHWvJF-SeDSxz2-Mjq3p%3DSHR3HvuJRAJJ3-A%40mail.gmail.com
> .
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegvopDTOWr2V4xf7gWPMVMiNSJbabiE5r48aYycg6iqRoQ%40mail.gmail.com.


[racket-users] Rosette: can it help me

2020-11-26 Thread Bertrand Augereau
Hello everybody,

not sure if this is the good place to ask about this but I couldn't
find a good location so I'm asking for help here, after all it's
strongly connected to Racket :)

I'm trying to reverse-engineer some specific piece of hardware with
the help of Rosette, to discover if it can help or not. In the long
run I want it to help me find magic numbers for a specific routine to
match captures of the hardware output (can't say more about this
though).

After dumbing down (a lot) my first tries, I'm having issues with this
code running endlessly:
#lang rosette/safe

(define (zero-a x) 0)
(define (zero-b x)
  (if (= x 0) 0 (zero-b (- x 1

(define-symbolic x integer?)
(assert (>= x 0))
(assert (<= x 10))

(verify (assert (= (zero-a x) (zero-b x

I don't have a strong theoretical understanding of what Rosette can
and cannot do, but I thought that by restraining the range with the
asserts, at least I'd get a brute force search of the solution space,
which should be fast.
Does it make sense ? Can somebody explain me where is my misunderstanding ?

Thank you folks,
Bertrand

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAHV%3D05otPXjbrEaHWvJF-SeDSxz2-Mjq3p%3DSHR3HvuJRAJJ3-A%40mail.gmail.com.


Re: [racket-users] snappier place startup time

2020-11-26 Thread nate
Awesome. Also I didn’t know you could include ‘#%place like that.

Nate

> On Nov 25, 2020, at 9:41 PM, Sam Tobin-Hochstadt  wrote:
> 
> Here's an in-progress PR: https://github.com/racket/racket/pull/3518
> 
> With this, your simple test takes about 150ms with `racket/place` and
> 50ms with `racket/place/dynamic`. For comparison, just having the
> submodule depend on `racket/base` gives a time of about 42 ms, and
> just `racket/kernel` is about 12ms.
> 
> Sam
> 
>> On Tue, Nov 24, 2020 at 2:04 PM Sam Tobin-Hochstadt
>>  wrote:
>> 
>> th-place is used if places are not enabled when Racket is built (this is the 
>> default on some platforms).
>> 
>> I'm making progress on shrinking this, hopefully I'll have a patch done soon.
>> 
>> One thing to note is that '#%place can be required directly and will have 
>> almost no start-up cost.
>> 
>> Sam
>> 
>> Sam
>> 
>>> On Tue, Nov 24, 2020, 1:58 PM Nathaniel W Griswold  
>>> wrote:
>>> 
>>> I noticed i am using the pl- functions so I replaced th-place.rkt with a 
>>> stub and saw more time shaved off, this time about 15ms for each 
>>> racket/place import.
>>> 
>>> Under what circumstances is th-place used instead of '#%place and needed?
>>> 
>>> Nate
>>> 
 On Nov 24, 2020, at 12:31 PM, Nathaniel W Griswold  
 wrote:
>>> 
>>> I seem to remember there being some global namespace. Since every 
>>> reasonable place will require racket/place, might it be possible to make 
>>> the racket/place import a special case and stick it in the global space, to 
>>> improve place setup time? It would be nice to be able to only set up 
>>> racket/place one time instead of once for each place.
>>> 
>>> Nate
>>> 
 On Nov 24, 2020, at 12:24 PM, Nathaniel W Griswold  
 wrote:
>>> 
>>> Actually, it cuts about 20-25ms off of a single import. Down from 185ms to 
>>> 165ms for me. 50ms off my startup time of my app on average, since i 
>>> basically stack the import twice and sync on the place being ready.
>>> 
>>> Might be worth including and seeing if there’s anything else that can be 
>>> shaved off.
>>> 
>>> Nate
>>> 
 On Nov 24, 2020, at 12:09 PM, Nathaniel W Griswold  
 wrote:
>>> 
>>> I checked into it a bit.
>>> 
>>> racket/fixnum, racket/flonum, and racket/vector are needed by 
>>> “private/th-place.rkt”, which is required by racket/place. Not sure why 
>>> DrRacket is saying that it’s not needed.
>>> 
>>> racket/runtime-path does not appear to be needed.
>>> 
>>> I tried removing racket/runtime-path and racket/match but didn’t see any 
>>> performance gains. It appears the delay is elsewhere.
>>> 
>>> Nate
>>> 
 On Nov 24, 2020, at 9:52 AM, Robby Findler  
 wrote:
>>> 
>>> DrRacket thinks that there are no references to a number of the requires in 
>>> racket/place, including racket/fixnum, racket/flonum, racket/vector, and 
>>> racket/runtime-path. Not sure if that's an error on DrRacket's part (and I 
>>> don't see why those would be needed for their effects).
>>> 
>>> Also, the only use of racket/match seems to be this, which seems simple to 
>>> rewrite out.
>>> 
>>>(match name
>>>  [(? symbol?) `(submod (quote ,name) ,submod-name)]
>>>  [(? path?) `(submod ,name ,submod-name)]
>>>  [`(,p ,s ...) `(submod ,(if (symbol? p) `(quote ,p) p) ,@s 
>>> ,submod-name)])
>>> 
>>> Robby
>>> 
>>> 
>>> On Tue, Nov 24, 2020 at 8:58 AM Nate Griswold  
>>> wrote:
 
 Oh, interesting. So compilation breaks the submodule out from the modules 
 if possible?
 
 So anyway, it sounds like breaking my modules out into separate files will 
 improve performance in most cases.
 
 Unfortunately, i need racket/place in the module that is my startup 
 bottleneck. If i modify the previous program to require racket/place and 
 compile, it takes around 180ms.
 
 This is about what i can expect for a module that requires racket/place, 
 then?
 
 Nate
 
 
 On Tue, Nov 24, 2020 at 8:48 AM Matthew Flatt  wrote:
> 
> Just to elaborate a little more:
> 
> The difference is because the `test` submodule can be loaded
> independently from the compiled form. Loading the submodule from source
> requires loading the enclosing module, too (which depends on
> `racket/place` and more).
> 
> At Tue, 24 Nov 2020 08:46:12 -0600, Nate Griswold wrote:
>> Awesome, thanks!
>> 
>> Nate
>> 
>> 
>> On Tue, Nov 24, 2020 at 8:44 AM Sam Tobin-Hochstadt 
>> 
>> wrote:
>> 
>>> Almost certainly the problem is expansion time. If I run that program
>>> on my machine, it takes about 200 ms. But if I compile the file to zo
>>> first with `raco make`, then it takes about 40 ms, basically identical
>>> to `racket/base`.
>>> 
>>> Sam
>>> 
>>> On Tue, Nov 24, 2020 at 9:39 AM Nate Griswold 
>>> wrote:
 
 Oops, i am having some issues with not getting to the list from my 
 other
>>> email 

Re: [racket-users] Passing keywords from a list

2020-11-26 Thread Dimaugh Silvestris
Thanks! Your answer led me to find keyword-apply, so I just wrote (I use 
def as abbreviation for define):

(def (parse-args xs)
  (def (fn ks kvs vs xs)
(cond [(empty? xs)  (map reverse (list ks kvs vs))]
  [(keyword? (car xs))
   (fn (cons (car xs) ks) (cons (cadr xs) kvs) vs (drop xs 2))]
  [else (fn ks kvs (cons (car xs) vs) (cdr xs))]))
  (fn '[] '[] '[] xs))

(def (func-args func args)
  (apply keyword-apply func (parse-args args)))

And now I can do 
:
(def (suma a b #:c [c 0]) (+ a b c))
(func-args suma (list 1 2 #:c 3))

and it works fine.

Thanks again!
On Thursday, 26 November 2020 at 10:06:26 UTC+1 sorawe...@gmail.com wrote:

> If you are OK with preprocessing the argument list into a dictionary, then 
> you can use keyword-apply/dict 
> 
> .
>
> For example:
>
> #lang racket
>
> (require racket/dict)
>
> (define proc (lambda (#:color color #:size size) (display (cons color size
> (define args '(#:color "red" #:size 3))
> (define args*
>   (for/hash ([chunk (in-slice 2 args)])
> (values (first chunk) (second chunk
>
> (keyword-apply/dict proc args* '())
>
>
> On Thu, Nov 26, 2020 at 12:53 AM Dimaugh Silvestris  
> wrote:
>
>> Is it possible to reproduce this behavior
>> ((lambda (#:color color #:size size) (display (cons color size))) #:color 
>> "red" #:size 3)
>> when what I have is a list such as '(#:color "red" #:size 3) ?
>> How can I feed keyword arguments stored in a list as symbols in a way 
>> that doesn't involve parsing manually?
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/06a1b45b-77ca-4595-b5b6-0b4ce01fe026n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/413ea474-0eac-4c7a-97a4-0c4ee711eb60n%40googlegroups.com.


Re: [racket-users] Passing keywords from a list

2020-11-26 Thread Sorawee Porncharoenwase
If you are OK with preprocessing the argument list into a dictionary, then
you can use keyword-apply/dict

.

For example:

#lang racket

(require racket/dict)

(define proc (lambda (#:color color #:size size) (display (cons color size
(define args '(#:color "red" #:size 3))
(define args*
  (for/hash ([chunk (in-slice 2 args)])
(values (first chunk) (second chunk

(keyword-apply/dict proc args* '())


On Thu, Nov 26, 2020 at 12:53 AM Dimaugh Silvestris <
dimaughsilvest...@gmail.com> wrote:

> Is it possible to reproduce this behavior
> ((lambda (#:color color #:size size) (display (cons color size))) #:color
> "red" #:size 3)
> when what I have is a list such as '(#:color "red" #:size 3) ?
> How can I feed keyword arguments stored in a list as symbols in a way that
> doesn't involve parsing manually?
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/06a1b45b-77ca-4595-b5b6-0b4ce01fe026n%40googlegroups.com
> 
> .
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegtRx--ZTxg0sNGb3475Pqb9vw_Hun%2BNp-A76SO8xGg1nQ%40mail.gmail.com.


[racket-users] Passing keywords from a list

2020-11-26 Thread Dimaugh Silvestris
Is it possible to reproduce this behavior
((lambda (#:color color #:size size) (display (cons color size))) #:color 
"red" #:size 3)
when what I have is a list such as '(#:color "red" #:size 3) ?
How can I feed keyword arguments stored in a list as symbols in a way that 
doesn't involve parsing manually?

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/06a1b45b-77ca-4595-b5b6-0b4ce01fe026n%40googlegroups.com.