[Haskell-cafe] One question...

Claus Reinke claus.reinke at talk21.com
Sat Dec 9 13:09:25 EST 2006

Hello yourself!-)

> Two days ago I have found Haskell in Internet. It sounds very nice.
> I have read some articles, few examples, ... yes it sounds nice.

you need to try it as well. haskell.org has information, tutorials, 
interactive implementations (and to answer the likely follow-on question:
you will usually need an editor and a Haskell implementation side by side;
edit your program in the editor, load the program into the implementation 
to play with), ..
> Now my problem is connected with the "non-update" object feature.
> I can't write "variable" instead object because - from the meaning of the word 
> ( variable ) - it has the possibility to CHANGE its value. 
> Yes that my problem :-(

a common (mis-)interpretation of "variable". a variable is a representation
of something that may not be constant, so far we are in agreement. but
the updateable variables you are referring to are a special case of variables,
those representing storage locations, the _contents_ of which may be 
it may be useful to think of variables as named placeholders for things,
that is, you either have the named placeholder, or you have the thing itself.
the process of replacing those placeholders with the things they stand for 
is called variable substitution.

update programming is all about communicating with some external store
of boxes (or objects), and updateable variables stand for references to 
such boxes (or identities of objects). your program is a sequence of
updates of the contents of those boxes - while the variables keep refering
to the same locations/references/object identities! executing an update
program modifies the external store by performing the prescribed updates,
step by step.

functional programming is all about replacing programs with programs, 
and variables are simply named placeholders in such programs. your 
program is a description of a problem, which may contain variables.
evaluating a functional program modifies the program, replacing program
parts by other program parts (and possibly variables by things), step by 

the aim of update programming is to transform a store whose contents
describe a problem into a store whose contents describe a solution.

the aim of functional programming is to transform a program which
describes a problem into a program which describes a solution.

non-declarative languages support very little functional programming
(although that is changing;-), so programmers mostly focus on update
programming. Haskell supports both functional programming and 
update programming, and programmers are encouraged to solve the
major part of their problems through functional programming, resorting
to update programming only where necessary.

and no, I haven't forgotten your problem;-) if you want to reap the
full benefits of learning functional programming, you need to adapt 
your way of thinking about programming to the additional flexibility 
that Haskell provides you with, and think of transforming programs 
into programs, rather than transforming stores into stores. variables
now stand for parts of the programs that you can fill in, not for parts
of the store that you can change.

in update programming, you need box variables, because the program
doesn't change, only the contents of the store change. in functional
programming, the whole program is transformed during evaluation -
most of the time, no need for an external store because everything
is variable.

> Let's assume that I've got to write the application that
> should works as follows: 
> 1. At the beginning the user should write her/his name (for instance as the 
> application parameter).
> 2. Let's assume that the application has many various functions defined 
> inside, and - after printing each of the outputs - the name has to be 
> printout.
> How should I think "properly" in Haskell to get described action?

Haskell implementations interleave the two phases of functional program
evaluation and update program execution (the latter most commonly
being input/output). in particular, the value of "Main.main" is expected
to evaluate to an update program, the first step of which is then executed,
feeding the result of that first step back into the functional evaluation
phase, and so on and so on..
you describe an application which requires a dialogue between program
and user, so you need for your program to produce such a dialogue as
the value of main. in fact, dialogue descriptions can be created and 
composed just like other functional values:

    main = do 
        name <- getName 
        let output = various name
        putStrLn output

    getName = do
      putStr "your name, please? "

    various name = "hello, "++name++" (that's "++show (length name)++" letters)"

note how the value of main is a dialogue, composed of an input phase
involving i/o, a computation phase involving no i/o, and an output phase
involving i/o again.

note also how "various" is the name of a placeholder variable standing
for a specific function, and how "name" is the name of a placeholder
variable standing for the user input. if you call "various" multiple times,
or if you run "main" multiple times, the same name in the same dialogue
and the same function will stand for different inputs, so the value of
the variable named "name" will vary.

but all of this talk only because you asked about how to think about
actions and variables in Haskell - as others have pointed out, the 
alternative is to dive right in, and to wait for the implications to become 
clear later, after a (possibly extended) while of practice.

hope this helps,

More information about the Haskell-Cafe mailing list