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

Q&A's, tips, howto's
Post Reply
fdb
Posts: 65
Joined: Sat Nov 09, 2013 8:49 pm

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

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

fdb
Posts: 65
Joined: Sat Nov 09, 2013 8:49 pm

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

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



pda
Posts: 10
Joined: Fri Sep 25, 2020 10:42 am

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

Post 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

HPW
Posts: 1379
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

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

Post 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
Hans-Peter

pda
Posts: 10
Joined: Fri Sep 25, 2020 10:42 am

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

Post 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

HPW
Posts: 1379
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

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

Post 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
Hans-Peter

Post Reply