Subject: RE: [hackers-il] Scheme in C Indentation Cont.

From: Omer Musaev <[email-masked]>
Date: Wed, 13 Mar 2002 11:25:44 +0200

> -----Original Message-----
> From: Shlomi Fish [mailto:[email-masked]]
> Sent: Wednesday, March 13, 2002 9:14 AM
> To: Hackers-IL
> Subject: [hackers-il] Scheme in C Indentation Cont.
> 
> 
> 
> I talked with the homework exercises checker of my "Structure and
> Interpretation of Computer Programs" course, and he told me that my
> exercises were very hard to follow. For a moment I got nervous that my
> code was bad, but then he told me that the reason was that I 
> placed every
> right parenthesis on its own line. Like this:
> 
> <<<
> (define (my-print-list mylist)
>     (define (helper idx mylist)
>         (if (not (null? mylist))
>             (begin
>                 (display idx)
>                 (display " ")
>                 (display (car mylist))
>                 (newline)
>                 (helper (+ idx 1) (cdr mylist))
>             )
>         )
>     )
>     (helper 0 mylist)
> )
> >>>

Of course this is bad code:)))
Here is good code:)

(define (f l)
   (map(let((i 0))(lambda(x)(printf "~a ~a~n" idx x)(set! i (+ 1 i))))l))

;;(joke)

> 
> Naturally, the problem is that _I_ find it easier to write, read and
> edit code like this. Plus, the lecturer said it was perfectly 
> OK for me to
> write it this way. And my grades for the homework turned out fine.

> I believe that I never saw the traditional Scheme
> indenendation as something that is inherent to the language, as I was
> already used to other indentation schemes. Other people OTOH are a bit
> "copy-cats" in the sense that they do exactly as they see it 
> and do not
> try to change the conventions. (This is not necessarily a bad thing)

Oh, man, now you insulted me. Copycat yourself!!!! Did you try to change
_YOUR_ conventions? 

> I read OmerM's post about why the traditional LISP/Scheme 
> indentation is like that. 

Here is continuation. Scheme is proud member of LISP family, this means
that for LISPer, Scheme is yet another LISP. However, there is something
inherent in CL that actually renders the "LISP" indentation style as 
preferable: the super-right-paren, or ']'

You use the ']' symbol in order to tell CL that you do not care how many 
right parens you have to put, but you do care that your top level form will
be closed. An example:

;;; ex 1
(defun foo nil "a stupid function with a lot of parens"
  (if (> 2 3)
      2
      (if (> 3 4)
       3
       (if (> 4 5)
        5
        ((lambda x x) 'x 'y' 'z)))))

;; ex 2
(defun goo nil "a stupid function with less parens"
  (if (> 2 3)
      2
      (if (> 3 4)
           3
           (if (> 4 5)
               5
               ((lambda x x) 'x 'y 'z ]

>I got his wave, but I still prefer it the C way, at least
> when I have to edit code. So, each to his own way.

Just to give you an idea how funny your code looks, lokittat:

int main (int i, char**v) {
        if(0){
           return 0; }
             else {
                return 1 ; } }
No more comments.

> Regards,
> 
> 	Shlomi Fish
> 
> 
> ----------------------------------------------------------------------
> Shlomi Fish        [email-masked]
> Home Page:         http://t2.technion.ac.il/~shlomif/
> Home E-mail:       [email-masked]
> 
> "Let's suppose you have a table with 2^n cups..."
> "Wait a second - is n a natural number?"
> 
> 
> To unsubscribe from this group, send an email to:
> [email-masked]
> 
>  
> 
> Your use of Yahoo! Groups is subject to 
http://docs.yahoo.com/info/terms/

---

From: Oleg Goldshmidt <[email-masked]>
Date: 13 Mar 2002 17:54:28 +0200

Shlomi Fish <[email-masked]> writes:

> I believe that I never saw the traditional Scheme indenendation as
> something that is inherent to the language, as I was already used to
> other indentation schemes. Other people OTOH are a bit "copy-cats"
> in the sense that they do exactly as they see it and do not try to
> change the conventions.

Of course it's "other people" who are not doing the right thing (tm).

There is often a reason for why things are conventionally done in a
certain way. Very rarely the reason is arbitrary. Think about why you
wanted to have parentheses on separate lines. Because that way it is
easier for you to see how they match, right? There is no need for
that, normally, because every lisp system in existence and most decent
general-purpose editors provide matching parentheses (brackets,
braces, etc) highlighting.

Note that the normal indentation in C matches not the opening
parenthesis, but the opening keyword, as in

if (status = under_nuclear_attack()) {
     full_retaliatory_strike();
}

In Lisp, there is no need for that, since everything begins and ends
with parentheses.

In C you often can flush syntax-related bugs because either
indentation or colorization (assuming a decent editor) is not what you
expect, and you sometimes can determine that there is a
design flaw if code just "does not look right". Same in lisp. A well
thought through code often looks like a "ladder" going from upper left
to lower right. Omer gave a couple of examples. Look at any lisp file
on your linux box (e.g. emacs sources) to see more involved examples.

Finally, learn my current .sig by heart ;-)

-- 
Oleg Goldshmidt | [email-masked] 
[Lisp] is the only computer language that is beautiful. 
                      - Neal Stephenson

---

From: Shlomi Fish <[email-masked]>
Date: Wed, 13 Mar 2002 20:22:56 +0200 (IST)

>    (map(let((i 0))(lambda(x)(printf "~a ~a~n" idx x)(set! i (+ 1 i))))l))

LOL. BTW, is printf standard in Scheme? Or is it present only in Common
Lisp?

> Oh, man, now you insulted me. Copycat yourself!!!! Did you try to change

What many people do ==> conventions
What I like to do ==> habit

I like my habits like they are now, from several reasons:

1. The cursor only jumps vertically and not horizontally.
2. I can tell how many scopes ended.

> (defun goo nil "a stupid function with less parens"

Nice trick. However, ] may prove non-useful at times, if I don't want to
close everything and still want ot add some arguments.

> int main (int i, char**v) {

LOL, again. However, I believe this code raises two more important
Questions of Life, the Universe and Everything<tm>:

1. If I wrote a purely functional code in Perl should I use the LISP style
or the C style?

2. If I wrote a non-functional (i.e: with assignment, "while" loops, etc)
code in Common Lisp, should it be in C style or in Lisp style?

3. If I wish to avoid those constant dillemas, should I just switch to
Python, where scoping is solely determined by indendtation and there are
no closing brackets?

:-)

Seriously now, I think the C style makes more sense for the common
denominator of C, Perl or LISP. I really hate those abrupt
back-indentations, because they make it hard for me to follow the code. (I
don't rely solely on the indentation)

Regards,

	Shlomi Fish

> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>

---

From: Shlomi Fish <[email-masked]>
Date: Wed, 13 Mar 2002 20:29:51 +0200 (IST)

> braces, etc) highlighting.

Are you aware of any way to turn it on in gvim? I can go to a matching
parens with "%", but I don't know how to highlight it as I type it.

> Finally, learn my current .sig by heart ;-)

I don't sure I buy into this whole argument. When LISP started
Text Editors were much more crude than they are today (think ed... or
punch-cards... %-)) and development environments were much less
sophisticated. Unless of course this style originated a lot less down the
line. (pardon someone who isn't an old timer for his lack of knowledge in
the minute details of the history of computing).

Regards,

	Shlomi Fish

---

From: Oleg Goldshmidt <[email-masked]>
Date: 14 Mar 2002 15:34:10 +0200

Shlomi Fish <[email-masked]> writes:

> Are you aware of any way to turn it on in gvim? 

Sorry, no - I don't know gvim. Is it a vi derivative? GNOME vim? I
don't know vi or any derivatives. I would be very surprised if modern
vi variants didn't have parenthesis matching or highlighting.

> punch-cards... %-)) 

Or Lisp Machines? ;-)