A note on assignment statement

55 views
Skip to first unread message

Dilawar Singh

unread,
Dec 24, 2013, 11:50:18 AM12/24/13
to wncc...@googlegroups.com
Those who say that Functional programming should be taught before the normal
programming (so called imperative programming such as C) to students to give a
better understanding of computation and programming often points out that
assignments (e.g. ``a = 5`` in python) can be avoided in programming to a large
extent. The authors of SICP do not introduce assignment till chapter 3. To be
sure, no all functional languages are without assignments but there are
functional languages which do not have 'assignments' such as Haskell. Haskell
has statement which just look like assignment e.g. ``let a = 5``. The syntax of
both statement does not look terribly different. But when we execute them in
their respective interpretor, the difference is evident. And something can be
learned by comparing both situation. First, Python:

$ python
>>> a = 5
>>> print a
5
>>> a = a + 1
>>> print a
6

We are all used to this. Now try to do the following in Haskell,

$ghci
Prelude> let a = 5
Prelude> let a = a + 1
Prelude> print a

We get stuck in infinite loop here. Why?

And that is the major difference between assignment in python and similar
looking let-statement in Haskell. The syntax looks almost same (lhs = rhs) but
the meaning of statements (semantic) differs greatly in both language.

Loosely speaking to illustrate the difference, when variable 'a' is assigned to
5, a fraction of memory is alloted and its addresses is bind to name `a`
(Compiler binds this location to variable 'a' which user can remember easily).
Now `a` can be thought of as the name of the address which has a value (state :=
(name, value)). When one does assignment, one fills/attach/bind the value in/to
that address. Assignment statement changes the value in memory location! This is
what they call mutability.

What is equivalent in Haskell if it does not have assignment. The let statement
(inside ghci) tells compiler that '5' is not value of 'a' but 'a' itself. Now
if I were to say ``let a = a + 1``, I am effectively telling compiler that ``5 +
1 = 5``. Which does not make sense, does it? And compiler should reject the
statement but it does not. So this is not what is happening here. Because ' 5 +
1' is not evaluated as soon as we insert it and compiler has no way to check for
the equality.

To understand this, we have to bring in lazy evaluation. The lazy evaluation
does not compute unless on demands the result. Therefore when we say ``let a = a
+ 1``, compiler does nothing and keeps the statement somewhere to be executed
later. When we demand it, the compiler tries to evaluate it. It searches for the
value of `a` to be substituted in rhs. It finds the statement ``let a = a + 1
``, and substitute 'a' by 'a + 1' in rhs and the new term in next iteration is
``a = (a + 1 ) + 1``. The rhs is still not reduced to minimal form and compiler
keeps substiting `a` with ``a + 1``, running into infinite loop.

Some languages have assignments which are different from the one we saw in
Python. For example, Hardware Description Language (HDL). In this view,
assignment not only update the value, they describe what some people call
'behaviour' (not to be confused with behavioural description in HDL). For
example,

>>> a = 5
>>> b = a + 1
>>> print b
>>> 6
>>> a = 7
>>> print b
>>> 6

The change in value of `a` does not change the value of `b`. The assignment
statement does not marry `b` with `a` in Python. In VHDL, however, assignment
statement like following does.

b <= a * 2 ;

It binds `b` to `a * 2`. And any change in a gets reflected in `b` according to
the given relation. This is natural way to describe electrical connections: `b`
is permanently connected to `a` in the same way output `b` of circuit is
permanently connected to input `a`.

-
Dilawar

Dilawar

unread,
Jan 11, 2014, 5:49:29 AM1/11/14
to wncc...@googlegroups.com
Guess what this python program prints and check you result after running it in interpretor. If you get it right, you get c like assignments right.
# python code start
fruit1
= "apples"
fruit2
= "oranges"
fruit3
= fruit2
fruit2
= fruit1
if (fruit3 == fruit2):
 
print 34
elif (fruit2 == fruit1):
 
print 72
elif (fruit3 == fruit1):
 
print 9
else:
 
print 119
Reply all
Reply to author
Forward
0 new messages