# [plt-scheme] Syntax question

 From: Benjamin L. Russell (dekudekuplex at yahoo.com) Date: Fri Jun 6 07:58:28 EDT 2008 Previous message: [plt-scheme] dynamic required modules and scheme/sandbox Next message: [plt-scheme] Vancouver Lisp Users Group meeting for June 2008 - Using PLT Scheme for Game Development Messages sorted by: [date] [thread] [subject] [author]

```(Andrew:  I think that you probably meant

(cons 2 '(1))

which yields

(2 1)

> (cons 2 1)

which yields

(2 . 1)

)

Arthur:

This is an interesting and unusual question for this mailing list; for a moment, I thought that was reading Haskell-Cafe by mistake!

To answer your question, yes, I agree with your conclusion.  IMHO, in general, cons (adding an element to the head of a list) should probably have a higher precedence than append (concatenating two lists).

(I think you made a typo when you wrote

> (2+1::2::1[]) + (0::[])

Notice that there is no '::' (cons in ML) operator in front of the first '[]' (empty list).  Therefore, I am assuming that you actually meant

(2+1::2::1::[]) + (0::[])

)

For the sake of simplicity, since I am personally not familiar with ML syntax, but happen to be familiar with both Haskell and Scheme syntax, I am going to translate your examples into both Haskell (which allows infix operators) and Scheme (which does not, but which is the programming language understood on this mailing list), and then investigate the implications of both assumptions.

Case 1:  Suppose that cons has higher precedence than append.

I.e., suppose that we prefer to write

(2+1)::2::1::[] + 0::[]

(2+1::2::1::[]) + (0::[])

Then:

(2+1) : 2 : 1 : [] ++ 0 : []

yields

[3,2,1,0]

(The list consisting of the elements 3, 2, 1, and 0.)

In Scheme:

(append (cons (+ 2 1) (cons 2 (cons 1 '()))) (cons 0 '()))

yields

(3 2 1 0)

(The list consisting of the elements 3, 2, 1, and 0.)

This is the current default behavior for both languages (Scheme and Haskell), as far as I know (Scheme uses prefix notation, and I was unable to find an operator precedence table for Haskell).  Since lists are composed of elements, the cons operator (prefix 'cons in Scheme (equivalent to (quote cons) in Scheme syntax), infix ':' in Haskell) takes one element and one list as arguments, and adds the element to the head of the list, whereas the append operator (prefix 'append in Scheme (equivalent to (quote append) in Scheme syntax), infix '++' in Haskell) takes two lists and concatenates the lists such that the elements of the first list precede the elements of the second list in the resulting list, so the operator precedence of this case example seems a natural operator precedence.

Case 2:  Suppose that append has higher precedence than cons.

I.e., suppose that we prefer to write

(2+1::2::1::[]) + (0::[])

(2+1)::2::1::[] + 0::[]

Then:

(2+1 : 2 : 1 : []) ++ (0 : [])

yields

[3,2,1,0]

(The list consisting of the elements 3, 2, 1, and 0.)

In Scheme:

(append (cons (+ 2 1) (cons 2 (cons 1 '()))) (cons 0 '()))

(no difference!)

yields

(3 2 1 0)

(The list consisting of the elements 3, 2, 1, and 0.)

Notice that Scheme's prefix notation yields equivalent expressions for both cases, regardless of operator precedence.  In the Haskell expression, in this case, an extra set of parentheses is now required on the right-hand side of the append operation.  Intuitively speaking, since the append operator takes arguments (lists) that are themselves composed of what are normally arguments (elements) of the cons operator, it seems more natural to assign higher operator precedence to the operator whose arguments are more elementary:  in this case, cons.

So, to conclude:  Yes.

Benjamin L. Russell

--- On Fri, 6/6/08, Andrew Reilly <andrew-scheme at areilly.bpc-users.org> wrote:

> From: Andrew Reilly <andrew-scheme at areilly.bpc-users.org>
> Subject: Re: [plt-scheme] Syntax question
> To: "Arthur Nunes-Harwit" <anh at cs.rit.edu>
> Cc: "PLT Scheme List" <plt-scheme at list.cs.brown.edu>
> Date: Friday, June 6, 2008, 9:02 AM
> On Thu, Jun 05, 2008 at 04:04:21PM -0400, Arthur
> Nunes-Harwit wrote:
> >   I am hoping to get unbiased opinions from this group
> concerning
> > questions of syntax.
>
> Kind of a strange forum to ask this question: I would have
> thought that most participants here would be more in favour
> of
> (cons 2 1) and (append '(2) '(1)), rather than
> operators with
> implicit precedance.  But if you're taking a poll, I
> prefer cons
> to have higher precedence than append, I think.
>
> Cheers,
>
> Andrew
> _________________________________________________