Sweet Expressions

I would like to use Lisp as a command-line prompt, and then expand it to be an operating system. A long-term, hopeless DamnFoolCrusade?, I know, but hey, I've always wanted to write my own operating system! I also have the impression that, as I work on tools to make Common Lisp a viable command line, it would make working with Unix (or other filesystems) easier, too.

Thus, I was interested in finding a way to write command prompts without parentheses, and have discovered SweetExpressions. More information, and code implementing them, is available at http://readable.sourceforge.net.

Quoting from the website http://www.dwheeler.com/readable/sweet-expressions.html :

Many people find Lisp s-expressions hard to read as a programming notation. This paper briefly describes a suite of three approaches I've developed: curly infix, modern-expressions, and sweet-expressions. These are not tied to any particular semantic, and can do everything regular s-expressions can do.

It should be emphasized that this work is being done by someone who has used Lisp for years, appreciates the power of Lisp macros, but is still uncomfortable with Lisp syntax. David Wheeler recognizes that previous attempts to fix syntax introduced complexities--in particular, it is a pain to write macros when you have infix with precedence--so everything he suggests is an attempt to improve syntax without interfering with the most important feature of Lisp.

Even so, most SmugLispWeenies have had fits over this proposal.

As for myself, I'm only an AspiringSmugLispWeenie?. I find the syntax attractive, in part because I like PythonLanguage, but also in part, I like the simplicity of EssExpressions, and the fact that these proposals map simply to EssExpressions indicates to me that there is very likely to be little impedance mismatch between this syntax and Lisp's macro system...but I really need to learn Lisp to discover if this is true. I am a mathematician, though, so the ideas of having infix in Lisp, and of having it map isomorphically to EssExpressions, are both attractive to me!

Ironically, Lisp already has a little bit of "function" syntax and "infix" syntax, adopted over the years: If I wanted to quote a list, I write '(1 2 3) rather than (quote (1 2 3)); the current Sweet-Expression proposal would expand this to include all functions. If I wanted to use CONS to create a linked list, or even other data structures, I would write '(A . (B . (C . NIL))) instead of '(CONS A (CONS B (CONS C NIL))) and '((A . B) (C . D) (E . F)) instead of '((CONS A B) (CONS C D) (CONS E F)).

Thus, at least a couple of the conventions proposed by this Sweet-Expression standard aren't new to Lisp. If these conventions are already a part of the language, (and are heavily used in the macro system, to boot!) I would think it's reasonable to see if these conventions can be expanded in an elegant way.

Without further ado, here are the basic rules for SweetExpressions:

Examples given by Wheeler.

S-exrpessions

   (define (fibfast n)
     (if (< n 2)
   n
   (fibup n 2 1 0)))

(define (fibup max count n-1 n-2) (if (= max count) (+ n-1 n-2) (fibup max (+ count 1) (+ n-1 n-2) n-1)))

(define (factorial n) (if (<= n 1) 1 (* n (factorial (- n 1)))))

Sweet-Expressions

   define fibfast(n)  ; Typical function notation
     if {n < 2}; Indentation, infix {...}
   n; Single expr = no new list
   fibup(n 2 1 0) ; Simple function calls

define fibup(max count n-1 n-2) if {max = count} {n-1 + n-2} fibup max {count + 1} {n-1 + n-2} n-1

define factorial(n) if {n <= 1} 1 {n * factorial{n - 1}} ; f{...} => f({...})

Wheeler doesn't give an example of how brackets might work, so I'll provide my own:

HASHTABLE["ITEM"] would translate to (bracketaccess HASHTABLE "ITEM"); the macro "BRACKETACCESS" should then translate it to (gethash "ITEM" HASHTABLE). (In my brief attempts to use hash tables in Common Lisp, I have found the interface to be incredibly awkward.)

'(1 2 3 4 5)[2] would translate to (bracketaccess '(1 2 3 4 5) 2), which would be properly become (nth '(1 2 3 4 5) 2).

--Alpheus

At one time only a Scheme implementation was available, but a Common Lisp implementation is now available. The "unsweeten" program (which translates sweet-expressions to s-expressions) can also work as a front-end preprocessor, so you can preprocess files or pipe its results into a REPL (like arc).

Also, you can pick just the first or first two tiers... so if you don't want indentation sensitivity, you can use just those tiers, or surround the whole expression with parens (which disable indentation processing).

Scheme users should probably see SRFI-105 and SRFI-110, which define these in more detail for Scheme. In Common Lisp you can install it easily using QuickLisp.

If you want to try it out, you can check out the tutorials at https://sourceforge.net/p/readable/wiki/Tutorial/


EditText of this page (last edited December 21, 2014) or FindPage with title or text search