Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-08-03 (13:26)
From: Francois Pottier <Francois.Pottier@i...>
Subject: [Caml-list] Re: your mail

> Someone wrote:
> > I would recommend not to use print_string as long as
> > you're not comfortable with the language; writing purely
> > functional code (no side effects) will be easier.
> I have always used print statements in programs to help me with
> debugging and to see what is going on (that is besides the necessity
> to get some output from the program).  A program with no side effects
> does not make sense to me.  What is the use of it?

I am the one who wrote that, so I'll clarify. Clearly, you are right
in thinking that `real' programs must have side effects. However, the
interactive environment (also known as the `toplevel loop') allows you
to evaluate expressions (that is, program fragments) which have no side
effects and to examine their result. This can be very useful. For
instance, consider the following interactive session:

  $ ocaml
	  Objective Caml version 3.02

  # let rec increment_list = function
    | [] ->
    | element :: rest ->
	(element + 1) :: (increment_list rest)
  val increment_list : int list -> int list = <fun>
  # increment_list [1;2;3];;
  - : int list = [2; 3; 4]

Here, I have defined a function of one argument, which accepts an
integer list and returns a list where all elements have been
incremented by 1. The function has no side effects (it does not
print anything), yet you can easily test it and see how it works.

I hope this helps,

François Pottier
Bug reports:  FAQ:
To unsubscribe, mail  Archives: