[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SRFI 105: Curly-infix-expressions



Shiro Kawai:
> I implemented curly-infix notation in Gauche and stated to give
> it a try.  And I can't help feeling c-exprs and s-exprs don't mix well. ...
> Here I pasted some code: https://gist.github.com/3502491
> I took exiting code that used some math (and I remember I wished
> to have had infix notation when I wrote them) and converted to C-exprs....
> I started itemizing why they don't mix, but before writing up
> a lenghthy email, I'd better check with you if I'm not doing it wrong.

I looked further at your pasted code (thanks for doing that).  To be honest, I don't think it's terrible at all, I think it's an improvement from traditional s-expressions.  From your description, and further looking, I'm guessing it's this kind of code you're unhappy about:

(define (S0 λ N)
  (sum-ec (: n 1 N)
          {λ * (exp (- {n * λ})) * (- {2 ^^ (ceiling (log n 2))}) * n}))

Again, I think this is a *BIG* improvement over traditional s-expressions.

I think as infix expressions get long they're less helpful.  Since you can CHOOSE when to use them, you can skip it, which is actually an *advantage* over many other languages with built-in infix. Try this:

(define (S0 λ N)
  (sum-ec (: n 1 N)
    (*
      λ
      (exp (- {n * λ}))
      (- {2 ^^ (ceiling (log n 2))})
      n)))

Now that said, its inability to handle stuff of the form f(...), such as -(...), is a big drawback.  We could add support for neoteric-expressions inside {...} to resolve that; it's an easy spec change, and that specification is mature.  So let's see what that would do:

(define (S0 λ N)
  (sum-ec (: n 1 N)
    {λ * exp(-({n * λ})) * -({2 ^^ ceiling(log(n 2))}) * n}))

That's better.  Again, kind of long for infix; try this:

(define (S0 λ N)
  (sum-ec (: n 1 N)
    (*
      λ
      exp(-{n * λ})
      -{2 ^^ ceiling(log(n 2))}
      n)))

If you don't like the mixing of () and {}, well, we intentionally designed it so that when you have 0 and 1 parameters you can use either.  That becomes (back to few lines):

(define (S0 λ N)
  (sum-ec (: n 1 N)
    {λ * exp{-{{n * λ}}} * -{{2 ^^ ceiling(log(n 2))}} * n}))

This compares favorably with full neoteric-expressions, which allow the use of f(x) for (f x) ANYWHERE:
define( S0(λ N)
  sum-ec( (: n 1 N)
    {λ * exp{-{{n * λ}}} * -{{2 ^^ ceiling(log(n 2))}} * n}))

Well, those are a lot of options.  In *practice*, what I would do is switch to a NON-infix form once the expression gets to be more than half a line with complex expressions inside them (as shown above).  So with curly-infix ONLY I'd do this (as shown above):
(define (S0 λ N)
  (sum-ec (: n 1 N)
    (*
      λ
      (exp (- {n * λ}))
      (- {2 ^^ (ceiling (log n 2))})
      n)))


--- David A. Wheeler