Browse thread
RE: [Caml-list] Great Beginner
- Krishnaswami, Neel
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Krishnaswami, Neel <neelk@c...> |
| Subject: | RE: [Caml-list] Great Beginner |
Franck [mailto:franck.collineau@francetelecom.com] 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
write:
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
done
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
neelk@cswcasa.com
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr