3 ms·
(I think you meant “if condition is false”) Technically you can accomplish the same thing in languages with first class functions if the caller wraps their cod
by janalsncm 3mo ago
(I think you meant “if condition is false”)
Technically you can accomplish the same thing in languages with first class functions if the caller wraps their code in a lambda.
- BeetleB 3mo agoNo, I meant "if condition is true". The point is that in most/all languages, if the condition is true, the else branch is not evaluated. And it's usually OK to put code in there that can crash when the condition is true, because we know it won't be evaluated. But if you make an if function like I did above in, say, Python, both the then_path and the else_path are evaluated before the decision is made. > Technically you can accomplish the same thing in languages with first class functions if the caller wraps their code in a lambda. True. For languages that have lambdas (which I guess is most of them nowadays).
- janalsncm 3mo agoOk yeah looking at it again I understand now. Still learning to read.
- gf000 3mo agoWell, as mentioned that's why you create two lambdas as the branches. This is pretty much what thunks are in some languages (like Haskell), so this example doesn't strictly require macros per se.
- KingMob 3mo agoThis is true, but thunks carry overhead that macros don't in this case.
- gf000 3mo agoWell, if we have a compiler that sees a constant value, it can completely optimize out one branch. But it of course depends on the semantics of the language. But yeah you are right in case of a naive compiler.
- BeetleB 3mo agoAnd I know people will think I'm crazy, but thunks are harder to read. (And I have trouble reading macros).