jcoo092
2020-4-30 07:09:08

:+1::skin-tone–2:


jcoo092
2020-4-30 07:09:11

Thanks :slightly_smiling_face:


samth
2020-4-30 13:54:14

In general, because of the way places work, you should have your uses of the place form be inside functions, not at the top level of the module.


samth
2020-4-30 13:54:59

Here’s an example places program I wrote recently: #lang racket/base (require racket/place) (define (load/place m n) (place/context c (begin (for ([i 10]) (printf "starting ~s ~s ~s \n" n i m) (define ns (make-base-namespace)) (parameterize ([current-namespace ns]) (time (dynamic-require m #f))) (printf "finished ~s ~s ~s\n" n i m)) (place-channel-put c m)))) (module+ main (map sync (list (load/place '(file "/home/samth/sw/plt/extra-pkgs/math/math-doc/math/scribblings/math.scrbl") 1) (load/place '(file "/home/samth/sw/plt/extra-pkgs/plot/plot-doc/plot/scribblings/plot.scrbl") 2) (load/place '(file "/home/samth/sw/plt/extra-pkgs/math/math-doc/math/scribblings/math.scrbl") 3) (load/place '(file "/home/samth/sw/plt/extra-pkgs/plot/plot-doc/plot/scribblings/plot.scrbl") 4) (load/place '(file "/home/samth/sw/plt/extra-pkgs/math/math-doc/math/scribblings/math.scrbl") 5) (load/place '(file "/home/samth/sw/plt/extra-pkgs/plot/plot-doc/plot/scribblings/plot.scrbl") 6) (load/place '(file "/home/samth/sw/plt/extra-pkgs/math/math-doc/math/scribblings/math.scrbl") 7) (load/place '(file "/home/samth/sw/plt/extra-pkgs/plot/plot-doc/plot/scribblings/plot.scrbl") 8) (load/place '(file "/home/samth/sw/plt/extra-pkgs/math/math-doc/math/scribblings/math.scrbl") 9) (load/place '(file "/home/samth/sw/plt/extra-pkgs/plot/plot-doc/plot/scribblings/plot.scrbl") 10) (load/place '(file "/home/samth/sw/plt/extra-pkgs/math/math-doc/math/scribblings/math.scrbl") 11) (load/place '(file "/home/samth/sw/plt/extra-pkgs/plot/plot-doc/plot/scribblings/plot.scrbl") 12))))


samth
2020-4-30 13:55:36

note that the use of place/context is in the outer module, but it is not at the top level of it.


samth
2020-4-30 13:57:50

As to what you should choose, futures are more for fine-grained parallelism, but are much less mature than places and are not as widely used. Places are more mature, but are also much more heavyweight and limited in what you can send between them.


c
2020-4-30 14:00:46

Hi all, silly question :slightly_smiling_face: I’m working through HTDP, and it’s asking me to place a nested cond into a statement. Here’s what I have got: (define (create-rocket-scene.v5 h) (place-image ROCKET 50 (cond [(<= h ROCKET-CENTER-TO-TOP) h] [else 50]) MTSCN)) It works fine, but to my eyes it looks ugly with the formatting the way it is. It’s hard to see that there’s that cond there doing work.

Did I format this idomatically? Should there be newlines somewhere?


mark.warren
2020-4-30 14:03:48

Personally I might do it like (define (create-rocket-scene.v5 h) (place-image ROCKET 50 (cond [(<= h ROCKET-CENTER-TO-TOP) h] [else 50]) MTSCN))


soegaard2
2020-4-30 14:03:57

It depends on the size of the cond-expression. If it is small, I would write as you do. But one could reformat it like this: (define (create-rocket-scene.v5 h) (place-image ROCKET 50 (cond [(<= h ROCKET-CENTER-TO-TOP) h] [else 50]) MTSCN))


soegaard2
2020-4-30 14:04:22

However I think most would make a local definition of the y-value:


soegaard2
2020-4-30 14:05:00

(define (create-rocket-scene.v5 h) (local [(define y (cond [(<= h ROCKET-CENTER-TO-TOP) h] [else 50]))] (place-image ROCKET 50 y MTSCN)))


soegaard2
2020-4-30 14:05:21

I hope I remember the correct notation for local.


c
2020-4-30 14:54:41

Awesome, thank you both :)


c
2020-4-30 14:59:56

Why would I use local and not let here? let seems to work I think


soegaard2
2020-4-30 15:01:02

Oh do use let. I only used local because it is used in htdp.


badkins
2020-4-30 15:02:42

@jcoo092 this doesn’t directly address your question, but in general, I’ve found that using one Racket process/place per CPU core for parallelism, and then making use of threads in each process is the best way to make the most of all CPU resources.


c
2020-4-30 15:08:01

Aha cool. Thank you! It is a bit tricky sometimes when BSL is missing some useful (simpler?) constructs from full-fat Racket, which is what you get when you hit Google


greg
2020-4-30 15:15:10

As much as I agree with and follow the advice to prefer cond to if, your example — an extremely simple, “inline” condition — is the 1% where honestly I myself would likely use if because the signal:noise ratio is higher: (if (<= h ROCKET-CENTER-TO-TOP) h 50).


greg
2020-4-30 15:16:09

But if you are going through HtDP, it is probably best to follow its learning path, because I am not a professional educator, and there is probably a reason for each step.


greg
2020-4-30 15:17:06

You can “suspend your disbelief” and know that writing real-world Racket programs might turn out to be slightly different. :slightly_smiling_face:


c
2020-4-30 15:18:42

Haha yes :) I’m using HTDP as a quarantine time to strip everything away and start again. I’ve been coding Go at work for the last 5 years, so it’s nice to do something else for a little bit and really think about what programming is a little bit again :)


greg
2020-4-30 15:19:10

It is called How to Design Programs, not exactly How to Write Racket Code.


c
2020-4-30 15:20:49

Indeed


greg
2020-4-30 15:21:32

Also I might define a little function, adjust-h (or some better name), and stick the if or cond or whatever there. The thing about if is that as soon as you get nested ifs it becomes less-clear pretty quickly. So the advice to use cond from the start, is generally pretty good advice.


c
2020-4-30 15:22:05

Yeah that was also something I was thinking about :thinking_face:


c
2020-4-30 18:24:45

Can you put a guard on a map function or do you have to have to use a for function?


notjack
2020-4-30 18:30:14

What do you mean by a guard?



c
2020-4-30 18:39:02

(for/list ([i (in-range 3)] #:when (odd? i)) i) ; '(1)


samth
2020-4-30 18:40:01

you might want filter-map


c
2020-4-30 18:40:07

Aha!


soegaard2
2020-4-30 18:40:33

Not exactly. But you can use filter first, then map. … Samth is fast :-)


c
2020-4-30 18:40:45

:rolling_on_the_floor_laughing: