[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: XML as a transition to s-expr




> Date: Tue, 18 Dec 2001 21:12:15 -0800 (PST)
> From: Paul Graham <paulgraham@yahoo.com>
> 
> Your reward for tuning out )))))))) is to be able to write
> macros.

Also, tuning out )))))))) is not hard if the program is formatted
correctly.   Here's an example from SICP (Abelson and Sussman's Structure
and Interpretation of Computer Programs, http://mitpress.mit.edu/sicp):

(define (list->tree elements)
  (car (partial-tree elements (length elements))))

(define (partial-tree elts n)
  (if (= n 0)
	  (cons '() elts)
	  (let ((left-size (quotient (- n 1) 2)))
		(let ((left-result (partial-tree elts left-size)))
		  (let ((left-tree (car left-result))
				(non-left-elts (cdr left-result))
				(right-size (- n (+ left-size 1))))
			(let ((this-entry (car non-left-elts))
				  (right-result (partial-tree (cdr non-left-elts)
											  right-size)))
			  (let ((right-tree (car right-result))
					(remaining-elts (cdr right-result)))
				(cons (make-tree this-entry left-tree right-tree)
					  remaining-elts))))))))

I'm not claiming that this code is trivial to read, but the )))))))) at the
end isn't a problem.  Nevertheless I can see that this might scare someone
new to programming.  Using a let* (which hadn't been introduced in the book
at that point IIRC) would have made it cleaner:

;; Warning: untested code:
(define (partial-tree elts n)
  (if (= n 0)
	  (cons '() elts)
	  (let* ((left-size (quotient (- n 1) 2))
			 (left-result (partial-tree elts left-size))
			 (left-tree (car left-result))
			 (non-left-elts (cdr left-result))
			 (right-size (- n (+ left-size 1)))
			 (this-entry (car non-left-elts))
			 (right-result (partial-tree (cdr non-left-elts)
										 right-size))
			 (right-tree (car right-result))
			 (remaining-elts (cdr right-result)))
		(cons (make-tree this-entry left-tree right-tree)
			  remaining-elts))))

and also reduced the terminating paren count.

> 
> I've found from experimenting with various syntactic tricks 
> in Arc that it's actually a win if all the delimiters are 
> parens.  It's a real nuisance to have expressions that end 
> ))})]), because you can't tell if you've put the } in the
> right place.
> 

Emacs will match [] and {} as well as ().  Don't other editors?  I don't
like the visual look of ))})]) but it's easier to grok some kinds of
statements when you have multiple delimiters e.g. (from the PLT scheme
distribution):

  (define read-lexer
    (lex
     [(: #\newline #\space #\tab (comment)) 
      (read-lexer lex-buf)]
     ["#t" 
      (token-BOOL #t)]
     ["#f" 
      (token-BOOL #f)]
	 ;; etc.

> What this suggests to me is that maybe one of the reason
> people don't write functional programs in languages with
> syntax is that the syntax gets in the way of it.  If so
> that's a second disadvantage of syntax (along with preventing
> macros).

??? What about standard ML, ocaml, and haskell?  They all have syntax and
are every bit as functional as lisp/scheme (in haskell's case, more so).
Now, if you'd said "languages with explicit syntax for block delimiters" I
might agree.

Mike