Page 1 of 1

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

Posted: Tue Oct 27, 2020 10:24 pm
by 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:

Code: Select all

(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:

Code: Select all

(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

Code: Select all

(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! ;-)

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

Posted: Wed Oct 28, 2020 9:34 pm
by fdb
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 ! ;-)

Code: Select all

(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)₄ 



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

Posted: Mon Dec 07, 2020 2:16 am
by pda
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

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

Posted: Mon Dec 07, 2020 8:25 pm
by HPW
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:

Code: Select all

(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

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

Posted: Fri Dec 25, 2020 12:30 am
by pda
HPW wrote:
Mon Dec 07, 2020 8:25 pm
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
HPW wrote:
Mon Dec 07, 2020 8:25 pm

Code: Select all

(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:

Code: Select all

(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

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

Posted: Fri Dec 25, 2020 6:18 am
by HPW
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