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
RE: [Caml-list] Great Beginner
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-11-09 (14:06)
From: Krishnaswami, Neel <neelk@c...>
Subject: RE: [Caml-list] Great Beginner
Franck [] wrote:
> Hello!
> I have written the following code:
> let somme(x,n)=
>     for i=1 to i=n
>         do
>         x<-x+1
>         done;;
> caml tel me that i is not define. What is the problem ?

You have a couple of syntax errors. First, the syntax of a 
for-loop is 

  "for i = 1 to n do <foo> done"

Second, your assignment statement x <- x + 1 has the wrong syntax,
and the wrong type. If you want to do an assignment, you need to 
use the (:=) operator, like this:

  x := 5

(:=) has the type 'a ref -> 'a -> unit, which means that the left
hand side should be a reference, and the right hand side should be
the value you want the reference to point to. However, when you 

  x := x + 1

you will get a type error, because x has type int ref, and (+) has
type int -> int -> int. So to do the addition you need to dereference 
the reference:

let somme(x, n) = 
  for i = 1 to n do
    x := !x + 1

which has type int ref -> int -> unit. This doesn't look like a 
function I would want to write, but maybe you do. :)

Neel Krishnaswami
Bug reports:  FAQ:
To unsubscribe, mail  Archives: