Page 1 of 1

Parens again, grumble grumble: an idea for manual notation

Posted: Wed Sep 30, 2009 8:36 am
by Christopher Oliver
This is not another I-hate-all-those-parens whinge whinge whinge posting. I use GNU emacs and SLIME, and even long before that I had made my peace with '(' and ')' when I sat at the computer. Contrary, I have found that when writing out even slightly hairy code on paper or at a whiteboard that I did have to count my parentheses to an annoying degree. Perhaps in that context it might be useful to subscript matching parentheses with some symbol and elide the earlier unmatched closing parentheses like this. (Lacking real subscripts, I'll fake it with TeX style.)

Example:

Code: Select all

(defun factorial (n) (if (< n 2) 1 (* n (factorial (1- n)))))
...becomes...

Code: Select all

(_0 defun factorial (n) (if (< 2 n) 1 (* n (factorial (1- n)_0
Hairy factorial which sadly looks better in a lisp-1 sans funcalls:

Code: Select all

(_0 funcall
	   (_1 lambda (r)
	     (funcall (lambda (f) (funcall f f))
		      (lambda (f)
			(lambda (n) (funcall (funcall r (funcall f f)) n)_1
	   (lambda (f)
	     (lambda (n) (if (< n 2) 1 (* n (funcall f (1- n)_0

Any thoughts?

Re: Parens again, grumble grumble: an idea for manual notation

Posted: Wed Sep 30, 2009 9:39 am
by nuntius
Outside of a computer, parens don't matter. They can often be omitted, decorated (e.g. with your subscripts), replaced by boxes or indentation, etc.

Do you need a computer representation of this, or are you just looking for ideas?

Re: Parens again, grumble grumble: an idea for manual notation

Posted: Wed Sep 30, 2009 8:03 pm
by Paul Donnelly
You could also use alternate types of brackets.

Re: Parens again, grumble grumble: an idea for manual notation

Posted: Fri Oct 02, 2009 9:01 am
by Christopher Oliver
nuntius wrote:Outside of a computer, parens don't matter.
I'm not sure I agree with this. If I wish to communicate the form of expressions, it seems to me I should show how they are delimited. Boxes and underbars are awkward, and indenting can get all too easily sloppy. Imagine you're showing someone Gabriel's Why of Y derivation, and you have only paper and pencil. It's all too easy to write expressions that aren't well formed. If you're explaining stuff at the same time, you're already walking and chewing gum.

Since my principal experience of lisp comes from reading code on line and in books, I've never seen subscripted parens used, so this is a new thought to me.
nuntius wrote: Do you need a computer representation of this, or are you just looking for ideas?
If a computer were involved, I wouldn't worry about a shorthand for paper or the whiteboard. What I'm interested in is considering a hand written notation for well formed s-exprs without paren counting. Good notation is a significant part of the battle whether for pedagogic or nmemonic purposes. I am also interested in exploring a possible explanation of why beginners often cite the parens as a reason to detest lisp.

Re: Parens again, grumble grumble: an idea for manual notation

Posted: Fri Oct 02, 2009 10:21 am
by gugamilare
I think I have an idea. First, write the code with only the necessary parens, like this:

Code: Select all

 defun fact (x)
   * x (fact (1- x))
For didactic purposes, I think this is already enough if you make clear for your students where the parens should be put.

If you are not satisfied with this solution, then you insert the parens one by one, first the outer parenthesis then the inner ones. Like this:

Code: Select all

  defun fact (x)
     * x (fact (1- x))

Code: Select all

( defun fact (x)
     * x (fact (1- x))   ) 

Code: Select all

( defun fact (x)
   ( * x (fact (1- x)) ) )