:+1::skin-tone–2:
Thanks :slightly_smiling_face:
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.
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))))
note that the use of place/context
is in the outer module, but it is not at the top level of it.
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.
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?
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))
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))
However I think most would make a local definition of the y-value:
(define (create-rocket-scene.v5 h)
(local [(define y (cond
[(<= h ROCKET-CENTER-TO-TOP) h]
[else 50]))]
(place-image ROCKET 50 y MTSCN)))
I hope I remember the correct notation for local
.
Awesome, thank you both :)
Why would I use local and not let here? let seems to work I think
Oh do use let. I only used local because it is used in htdp.
@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.
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
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)
.
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.
You can “suspend your disbelief” and know that writing real-world Racket programs might turn out to be slightly different. :slightly_smiling_face:
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 :)
It is called How to Design Programs, not exactly How to Write Racket Code.
Indeed
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 if
s it becomes less-clear pretty quickly. So the advice to use cond
from the start, is generally pretty good advice.
Yeah that was also something I was thinking about :thinking_face:
Can you put a guard on a map
function or do you have to have to use a for
function?
What do you mean by a guard?
(for/list ([i (in-range 3)]
#:when (odd? i))
i) ; '(1)
you might want filter-map
Aha!
Not exactly. But you can use filter
first, then map. … Samth is fast :-)
:rolling_on_the_floor_laughing: