Arc Forumnew | comments | leaders | submitlogin
3 points by fallintothis 4566 days ago | link | parent

Looking for a vanilla Arc solution, if

  (whenlet var expr ...)
is the same (modulo multiple evaluation) as

  (when expr
    (let var expr ...))
then it kind of makes sense to call

  (let var expr
    (when (f var) ...))
the same name in the opposite direction, like

  (letwhen (f var) var expr ...)
I.e.,

  (mac letwhen (test var expr . body)
    `(let ,var ,expr
       (when ,test ,@body)))

  (letwhen (even x) x 50
    (prn "I print"))

  (letwhen (even (+ x 1)) x 50
    (prn "I don't print"))
Alas, it's very clunky to read. I think Haskell makes this sort of code look nice, because you can have a postfix variable declaration, like

  blah = if (f var) then ... else ...
    where var = expr
So, maybe something like

  (when (f var) :where var = expr
    ...)
would do away with the extra level of indentation.


3 points by akkartik 4566 days ago | link

1. I like your final solution! But no reason why either of ours couldn't be done in vanilla arc.

2. (whenlet var expr ...) could also be written:

  (let var expr
    (when var ...))
So I don't think letwhen is strictly necessary.

3. Another alternative formulation is:

  (let var expr :when (f var)
    ...)
All that's really saving us is a couple of parens compared to the original pattern. Maybe we don't need a macro, just a different indentation convention:

  (let var expr (when (f var)
    ...))
But both these make the conditional nature of the block harder to see. My original solution and yours don't have that drawback.

-----