
Hi folks in C and C++ world the humble comma is an operator. Is this also the case in Haskell or, is it classed as a function? In the wikibook they talk about consing new elements onto a list. Does this cons stand for anything meaningful in the English language? For example, is it short for construction or something similar? I'm assuming that the ":" function takes two arguments and returns a newly constructed list which is assigned to the variable holding/pointing to the old list. Cheers Paul

On Feb 26, 2007, at 21:27 , P. R. Stanley wrote:
Hi folks in C and C++ world the humble comma is an operator. Is this also the case in Haskell or, is it classed as a function?
Neither; it's syntactic sugar. The formal tuple constructor is (, [,...]) a [b ...] (e.g. (,,) a b c), with (a, b, c) a friendlier syntax for it, just as [a, b, c] is syntactic sugar for a : b : c : [].
In the wikibook they talk about consing new elements onto a list. Does this cons stand for anything meaningful in the English language? For example, is it short for construction or something similar?
It's "construct", but the term was actually borrowed from Lisp. Count yourself lucky Haskell doesn't use the related Lisp terms "car" and "cdr", which are named after registers in some ancient IBM CPU architecture. Haskell went with the saner "head" and "tail" instead.
I'm assuming that the ":" function takes two arguments and returns a newly constructed list which is assigned to the variable holding/ pointing to the old list.
First part correct, second quite wrong --- Haskell doesn't have destructive assignment in the general case. There are special cases, such as IORefs and MVars and TVars, but Haskell puts restrictions on how you go about using such impure operations. -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

I'm assuming that the ":" function takes two arguments and returns a newly constructed list which is assigned to the variable holding/ pointing to the old list.
First part correct, second quite wrong --- Haskell doesn't have destructive assignment in the general case. There are special cases, such as IORefs and MVars and TVars, but Haskell puts restrictions on how you go about using such impure operations.
You know, as soon as I posted the message I remembered the destructive assignment thingummy. the following is what I was talking about: Prelude> 13:[1, 2] [13, 1, 2] which I don't believe has an address in the memory, correct? Back to the comma, surely, syntax sugar fulfills the role of an operator, a function, or a sequence of low-level procedures, either in part or comprehensively. In C, for example, iteration could be implemented using the if construct with the dreaded goto command. So, strictly speaking, the while loop could be classed as syntax sugar. Yet, the while loop is a well-recognized construct in its own right. I hope you can see what I'm driving at. Paul

P. R. Stanley wrote:
You know, as soon as I posted the message I remembered the destructive assignment thingummy. the following is what I was talking about: Prelude> 13:[1, 2] [13, 1, 2] which I don't believe has an address in the memory, correct?
No. It does have a well-defined address in memory, which is there as long as you need it (and then garbage-collected away sometime thereafter).
Back to the comma, surely, syntax sugar fulfills the role of an operator, a function, or a sequence of low-level procedures, either in part or comprehensively.
The ":" is actually a constructor, which is a function that is fully evaluated at compile time and on which you can do pattern matching. The list syntax also forms such a pattern.
In C, for example, iteration could be implemented using the if construct with the dreaded goto command. So, strictly speaking, the while loop could be classed as syntax sugar. Yet, the while loop is a well-recognized construct in its own right. I hope you can see what I'm driving at.
Syntactic sugar is fully desugared at compile time. A while loop with constant limits *could* be considered syntactic sugar if the compiler can statically unroll the loop. Variable limits are definitely beyond this definition, since they can only be evaluated at runtime. Dan Weston

On 2/26/07, Dan Weston
P. R. Stanley wrote:
In C, for example, iteration could be implemented using the if construct with the dreaded goto command. So, strictly speaking, the while loop could be classed as syntax sugar. Yet, the while loop is a well-recognized construct in its own right. I hope you can see what I'm driving at.
Syntactic sugar is fully desugared at compile time. A while loop with constant limits *could* be considered syntactic sugar if the compiler can statically unroll the loop. Variable limits are definitely beyond this definition, since they can only be evaluated at runtime.
I think what P.R. meant is that while loops in C can be desugared like this: while(condition) { stuff } more_stuff => loop: if(!condition) goto exit; stuff goto loop; exit: more_stuff No static unrolling of the loop happens. So in the sense of "syntactic sugar" that means "a construct that can be defined in terms of other language constructs", while is syntactic sugar in C. Not sure what the original point was, though. Cheers, Kirsten -- Kirsten Chevalier* chevalier@alum.wellesley.edu *Often in error, never in doubt "Are you aware that rushing toward a goal is a sublimated death wish? It's no coincidence we call them 'deadlines'." -- Tom Robbins

On Feb 26, 2007, at 22:17 , P. R. Stanley wrote:
Prelude> 13:[1, 2] [13, 1, 2] which I don't believe has an address in the memory, correct?
If I understand what you're getting at: internally it just allocates a new cons cell, stuffs 13 in the left side and a pointer to the existing list [1, 2] in the right side, yes.
Back to the comma, surely, syntax sugar fulfills the role of an operator, a function, or a sequence of low-level procedures, either in part or comprehensively.
I suppose I'd have to go with the latter. In the formal constructor syntax (,,) it's just part of the operator name, but the tuple constructors are unique in that they don't need to be predeclared --- Haskell just looks for a parenthesized series of commas and counts the commas to find out the size of the tuple. (Note that the unit type () falls out of this as a degenerate case.) (1, 2, 3) is internally rewritten to (,,) 1 2 3, then parsed as the tuple constructor (,,) applied to the three arguments that constructor requires: Prelude> :kind (,,) (,,) :: * -> * -> * -> * Prelude> :type (,,) (,,) :: a -> b -> c -> (a, b, c) My point is that, syntactically, the comma can't really be considered a function or an operator per se; it's just special syntax. -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On 2/26/07, P. R. Stanley
Back to the comma, surely, syntax sugar fulfills the role of an operator, a function, or a sequence of low-level procedures, either in part or comprehensively. In C, for example, iteration could be implemented using the if construct with the dreaded goto command. So, strictly speaking, the while loop could be classed as syntax sugar. Yet, the while loop is a well-recognized construct in its own right. I hope you can see what I'm driving at.
It's useful to see the square-bracket-and-comma list notation in Haskell as syntactic sugar because you don't need to worry about what it means; you just need to know how to mentally translate it into applications of cons and nil, and you already know what those means. Indeed, Haskell compilers are based on that same principle. Cheers, Kirsten -- Kirsten Chevalier* chevalier@alum.wellesley.edu *Often in error, never in doubt "Apathy at the individual level translates into insanity at the mass level." -- Douglas Hofstadter
participants (4)
-
Brandon S. Allbery KF8NH
-
Dan Weston
-
Kirsten Chevalier
-
P. R. Stanley