Get rid of (some of) the parentheses with sublisp!

Started by fdb, October 27, 2020, 03:24:55 PM

Previous topic - Next topic

fdb

I really like (new)lisp and even the parentheses but they can get kind of annoying, especially at the end of function so in a flash of insight I got the idea to replace  the right parentheses with a subscripted count if there is more then one, see below original code:

(define (initial-clauses str)
  (set 'start (array rows columns (map int (explode str))))
  (for (row 0 (sub1 rows))
    (for (col 0 (sub1 columns))
      (when (> (start row col) 0)
        (push (string (to-var row col (sub1 (start row col))) " 0")
               problem)))))

And below if run through sublisp:

(define (initial-clauses str)
  (set 'start (array rows columns (map int (explode str)₄
  (for (row 0 (sub1 rows)₂
    (for (col 0 (sub1 columns)₂
      (when (> (start row col) 0)
        (push (string (to-var row col (sub1 (start row col)₃ " 0")
               problem)₅

And the code run through itself

(define (sublisp txt)
  (set 'offset 0x2080)
  (for (x 9 2 -1)
    (replace (dup ")" x)
             txt
             (string ")" (char (+ offset x)₅
   (print (string txt)₂
   nil)


Now if someone hacks into newlisp to change this back before it gets translated you could even execute it!  ;-)

fdb

#1
As a final  'pièce de résistance' a much better and faster convert function (down) and one to add the parentheses (up, watch the regex option/number), also file versions and of course in the sublisp syntax ! ;-)



(define (down txt)
  (replace {))+}
             txt
             (string ")" (char (+ 0x2080 (length $it)₄
             0)₂

(define (up txt)
  (replace {)[₂|₃|₄|₅|₆|₇|₈|₉]}
            txt
            (dup ")" (- (char ($it 1)₂ 0x2080)₂
            2048)₂
             
(define (down-file file)
  (write-file file
    (down (read-file file)₄

(define (up-file file)
  (write-file file
    (up (read-file file)₄



pda

#2
pretty interesting but just for pretty printing newlisp code, it's not a notation for code input because is harder than real one and also clumsy since you have to keep the count of opened parens in your head to write the right upper number.



more interesting would be an editor automatically doing the pretty printing or even better the newlisp REPL with some kind of activation

HPW

#3
Hello,

I have no problem with the paranthesis and they belong to every lisp.

For me a good editor solves the problem with paranthesis checker.

Also a different coding style can help to view the structure better:


(define (initial-clauses str)
  (set 'start (array rows columns (map int (explode str))))
  (for (row 0 (sub1 rows))
    (for (col 0 (sub1 columns))
      (when (> (start row col) 0)
        (push (string (to-var row col (sub1 (start row col))) " 0")
               problem
        )
      )
    )
  )
)


With the proper indention you see which paranthesis belog to the opening paranthesis.



Regards
Hans-Peter

pda

#4
Quote from: HPW post_id=24974 time=1607372710 user_id=4
I have no problem with the paranthesis and they belong to every lisp.

For me a good editor solves the problem with paranthesis checker.


I agree but this proposal is a funny one and may be interesting as an option in IDE preferences for pretty printing


Quote from: HPW post_id=24974 time=1607372710 user_id=4
(define (initial-clauses str)
  (set 'start (array rows columns (map int (explode str))))
  (for (row 0 (sub1 rows))
    (for (col 0 (sub1 columns))
      (when (> (start row col) 0)
        (push (string (to-var row col (sub1 (start row col))) " 0")
               problem
        )
      )
    )
  )
)

With the proper indention you see which paranthesis belog to the opening paranthesis.


that indentation is far away from proper indentation,  historically well stablished proper indentation for that code is:

(define (initial-clauses str)
   (set 'start (array rows columns (map int (explode str))))
   (for (row 0 (sub1 rows))
     (for (col 0 (sub1 columns))
       (when (> (start row col) 0)
         (push  (string (to-var row col (sub1 (start row col))) " 0")  problem )))))


with minor variatons as a matter of style

HPW

#5
Hello,



I agree that it is a matter of style and taste.

The good thing is that a lisp does allow every user to do it in his preferred style. (not like python for example)

My use of that style comes from a practical view in using it in a huge amount of production code in autolisp/newlisp.

My editor does not only support the paranthesis checker, but disolay a small vertical line from opening paranthesis to the closing one in real time.

And copy and paste for code blocks becomes vey easy when the code is seperated on different lines.



Regards

Hans-Peter
Hans-Peter