5 ms·
I've always wondered if a good way to do infix would be to simply have the ability to mark an item with a character that means "move this to the front of the ex
by a_lieb 7y ago
I've always wondered if a good way to do infix would be to simply have the ability to mark an item with a character that means "move this to the front of the expression." Let's say the char is ":". Then this:
(1 :+ 2)
Would be converted to this:
(+ 1 2)
It would also allow for arbitrary placement, so you can do goofy stuff like pretend to be Forth without writing custom macros for the situation:
(1 2 3 4 5 :+)
Whether or not that part is an advantage is another story ;)
This doesn't solve the "too many parens" problem, because you still have parens, but it does solve the "prefix math looks weird" problem. It's quite a bit simpler than what Dylan does, in that there's no special concept of "binary operators" and it's obvious at a glance how everything converts to sexps.
- hyperion2010 7y agoRacket currently has something like this for certain operators. `. -> . ` will allow the arrow to sit in between inputs and outputs in contracts.
- darkpuma 7y ago> for certain operators AFAIK it works for any operator that takes two operands (or more, but it makes less sense to be using it with three or more operands.) e.g: > '(arg0 . operator . arg1 arg2) '(operator arg0 arg1 arg2)
- a_lieb 7y agoAh, interesting—thanks for pointing that out. That is practically the same thing. I only wonder if the fact that it's two dots tends to discourage it from general use for things like arithmetic, compared to a single character.
- slaymaker1907 7y agoYou could do this in Racket by messing with #%app. Supporting arbitrary infix operators could be a bit tricky, but just doing standard math ones wouldn’t be too difficult.
- breck 7y agoThat’s an interesting idea. “Markfix” Notation? How would it work if you had 2+ operators in one line?
- a_lieb 7y agoGood question--just stack them up on the left on the same order as they appear in the expression, I guess.