[Haskell-cafe] Parameters and patterns
Richard O'Keefe
ok at cs.otago.ac.nz
Mon Oct 3 07:06:14 CEST 2011
On 2/10/2011, at 3:27 AM, José Romildo Malaquias wrote:
> Hello.
>
> When studing programming languages I have learned that parameter is a
> variable (name) that appears in a function definition and denotes the
> value to which the function is applied when the function is called.
Who told you that? Variables are one thing and names are another.
I think you are talking about what the definition of Algol 60
called a "formal parameter".
It's worth noting that formal parameters in Algol 60 could be
labels, switches, and procedures, while there were no label variables,
switch variables, or procedure variables. Names and variables are
*really* different things.
For what it's worth, the Haskell 2010 report appears to use "parameter"
informally to mean
- a formal parameter of a function
- a formal parameter position of a type constructor
- a constant characterising a numeric type
but I don't see a precise definition anywhere.
>
> Argument is the value to which the function is applied.
I think you are talking about what the definition of Algol 60
called an "actual parameter".
The word "argument" is very often used for formal parameters too.
For what it's worth, the Haskell 2010 report appears to use "argument"
informally to mean
- an actual parameter of a function
- an actual parameter of a type constructor
but I don't see a precise definition anywhere.
> Now I am not sure how to apply these concepts to Haskell, as Haskell
> uses pattern matching to deal with argument passing to functions.
Realise that what you thought you knew was a half truth: all formal
parameters are patterns, and all (new) identifiers are patterns, but
not all patterns are identifiers. (And of course that is a half truth
too.)
>
> For instance, in the definition
>
> f x = 2 * x + 1
>
> x is a parameter, and in the application
a parameter, or an argument, or a formal parameter, or a formal
argument, or what you please.
>
> f 34
>
> 34 is an argument
an argument, or a parameter, or an actual parameter, or an
actual argument, or what you please.
>
> But in the definition
>
> g (_:xs) = xs
>
> what is the parameter of the function g? Is it the pattern (_:xs)? If so
> then a parameter is not necessarily a variable anymore, and that seems
> very strange.
Why? Patterns are a generalisation of variables.
Practically all functional languages since lisp use pattern matching,
and even Lisp these days has destructuring-bind.
> And what is xs? Is it a parameter, although it does not
> denote the value to which the function is aplied, but just part of it?
This is the point where some people would say "this is just semantics".
The problem is that it is precisely NOT semantics. You clearly understand
the *semantics* here; what's bothering you is the lexical level, what to
call something. The first occurrence of xs is "a binding occurrence of an
identifier inside a formal parameter" and the second occurrence is "an
applied occurence of an identifier".
The Haskell 2010 report often uses "parameter" to refer to a
formal parameter _place_ of a function rather than to the text
that fills that place in a function definition. On that reading,
(_:xs) is *not* a parameter, it's a pattern that appears in the
first parameter *position* of g, and parameters as such do not have names.
> I am writing some slides to use in my functional programming classes,
> but I am not sure how to deal with these terms.
Consistently with the text-book.
More information about the Haskell-Cafe
mailing list