For Programmers: Free Programming Magazines  


Home > Archive > Functional > July 2007 > Re: Newbie Question: Is it allowed for the function to change the









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Newbie Question: Is it allowed for the function to change the
Markus E Leypold

2007-07-25, 8:05 am


> Hi,
>
> I am really new to functional programming. I am just wondering if it
> is theoretically allowed to for the function to change the value of
> the parameters?


Short (and useless) answer: That depends on the language in
question. Wether then it's stilled called "functional" depends on
who's arguing :-).

Longer answer: Forget the term 'variable' for a moment. Let's talk
about 'names' (identifiers) and the things behind them.

In a pure functional language names denote values (like 5, 10 or
[2;3], the latter being one value in the big set of all integer
lists). You can name values by saying things like 'let foo = some
expression in ...') which means that the value to which 'some
expression' evaluates should be known under the name foo in the
section indicated by '...'. In functional languages a program just
produces values from other values by evaluating expressions. Names are
a convenience to refer to values (and necessary to name parameters,
but let's ignore that for a moment).

In imperative languages names do NOT denote values, but usually are
bound to storage places, i.e. containers for values. An assigment 'a =
....' puts a different value in the container, but note that the
container is still the same! When using a name in an expression, it is
automatically 'dereferenced', that is, it is taken to mean the value
in the container rather then the container (address) itself. Therefore
we can write

b = 3 * a

instead of

b = 2 * (get_value(a))

or something like this.

So strictly speaking: If the language is (purely) functional you
cannot change variables: There are no variables, just names which are
(during evaluation of a particular subexpression) are 'bound' to
certain values, meaning supposed to stand for certain values that have
been evaluated earlier.

> Haskell's list seems to be immutable so it is not a problem. What if I
> use python:
>
> def f(list):
> list.append("a")
>
> will change the list passed in. Is this generally acceptable practice
> in functional programming?


Depends. There is a wide spectrum of languages called functions: From
Scheme, Lisp, Ocaml to Haskell. Pure functional languages don't allow
side effects (as what you give as example). In the other languages
side effects are usually used to make things more efficient (esp. low
level interfacing with the outside world or real time critical loops)
but the general advice is, to know what you're doing: Don't use side
effects if you're not sure you really need them. Don't optimize
prematurely.



>
> How about this:
>
> list=[]
> f= lambda x: list.append(x)
>
> Now I just defined a function that has side effects?


Still Python?

What actually is the question here? Wether this is good? => If you can
live with it, fine. In Python you probably also have to use idioms
with side effects. But the adivice is, to avoid side effects in
functional programming.

Regards -- Markus
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com