Fixing Lisp Syntax

2019-03-11 03:34发布

Being a newbie to Lisp I'm wondering if the Lisp syntax could be "fixed"?

Some people say the syntax in Lisp is one of its biggest strengths. I don't quite understand this.

Isn't it possible to replace "obvious" parentheses with a combination of white spaces, new lines and indenting? Just like in Python?

It looks to me like parentheses are the most used characters in Lisp code. I'm wondering if that's true - but if it is, isn't this a suggestion, that there is some redundancy in the syntax?

Is there some simple answer to the question - why so many parentheses?

For example:

(defun factorial (x)
    (if (= x 0)
        1
        (* x 
           (factorial (- x 1)))))

Why not:

defun factorial (x)
  if (= x 0)
    1
    * x
      factorial
        - x 1

e.g. close parentheses at the end of line, and always open them on new lines. Only the 1 would be ambiguous - is it 1 or (1) - but we could introduce an exception - single tokens are not "listified".

Could this work?

Edit:

Thank you all! I see now there are some links at the lispin site.

16条回答
唯我独甜
2楼-- · 2019-03-11 04:05

My personal opinion on this is: don't use newlines and indentation as "implicit" separators (I also don't like implicit grouping by operator precedence, but that is a different, although related topic).

Example: Is this valid Python? Does it do what you expect?

if foo < 0 :
    bar
  else :
    baz
quuz

I have a little proposal: we could use a preprocessor to indent Python code automatically, by putting special characters into the code that tell it where a statement begins and ends. How about parentheses for this?

查看更多
一夜七次
3楼-- · 2019-03-11 04:09

You could take a look at Clojure. Lesser parentheses but more syntax to remember.

查看更多
贼婆χ
4楼-- · 2019-03-11 04:10

This thread is old, but I've decided to contribute, because I've created a very similar reader system.

EDIT: Some samples of usage:

!defun fact (n)
  if (zerop n)
    1
    * n
      fact
        (1- n)
!defun fact !n
  if !zerop n
    1
    !* n !fact !1- n

For more information check the homepage of project.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-03-11 04:11

To answer the question of why:

It's main use as far as i know is readability, so that people who look at your code can decipher it more easily.

Hope it helps.

查看更多
登录 后发表回答