This might not be a good fit for threading macros.

If you need to calculate some extra, intermediate values, I
definitely wouldn't recommend `set!`-ing them on the side in a
threading macro (if that's what you had in mind).

Threading macros produce a single value.

If you need to produce multiple values, you could aggregate them
into a single value like a `struct` or `list`, and thread _that_.
Assuming you have functions that take it.

But if you have "intermediate" values that are undefined until a
certain point in the calculation? It sounds like you simply want
to use named variables for each step of the calculation.

(define principal 10.0)
(define rate 0.05)
(define interest (* principal rate))
(define return (+ principal interest))
(define flat-fee 1.00)
(define net-return (- return flat-fee))


Maybe you don't like typing `define` so much? Or, maybe many of
the steps don't have an obvious meaningful name? In that case, an
alternative to a threading macro is the "classic" `let*` idiom
where you keep shadowing the same short, meaningless name:

(let* ([v 10.0]
       [v (v * 1.05)]
       [v (* v rate)]
       [something-else (+ v 42)]
       [v (* v 100)])
  ;; use `v` and `something-else`
  )

Each line binds a new `v` that shadows the previous one. (In
something like `[v (v * 1.05]` the `v` on the right-hand side is
still the value from the previous binding.)

Whether this is clearer is a matter of taste.


p.s. If you're dealing with money, of course don't use floating
point like I did above! (Because rounding errors.)

-- 
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.

Reply via email to