caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Great Beginner
@ 2001-11-09 13:44 Franck
  2001-11-11 11:34 ` Remi VANICAT
  0 siblings, 1 reply; 7+ messages in thread
From: Franck @ 2001-11-09 13:44 UTC (permalink / raw)
  To: caml-list

Hi!

I want to calculate the sum of the n first numbers.
I've written this code:
let x =0;;
let somme n =
        for i=1 to n
                do
                  x=x+i
                done;;

It doesn't work.
Why ?

Thanks

Franck

-- 
___________________________________
Franck Collineau
FTR&D DMI/TAM/TIL
4 rue du Clos Courtel
B.P. 59
35512 Cesson Sévigné Cedex
tél: 02 99 12 43 97
franck.collineau@francetelecom.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


^ permalink raw reply	[flat|nested] 7+ messages in thread
* RE: [Caml-list] Great Beginner
@ 2001-11-09 14:10 Krishnaswami, Neel
  0 siblings, 0 replies; 7+ messages in thread
From: Krishnaswami, Neel @ 2001-11-09 14:10 UTC (permalink / raw)
  To: 'Franck', caml-list

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


^ permalink raw reply	[flat|nested] 7+ messages in thread
* [Caml-list] Great Beginner
@ 2001-11-09 11:08 Franck
  0 siblings, 0 replies; 7+ messages in thread
From: Franck @ 2001-11-09 11:08 UTC (permalink / raw)
  To: caml-list

    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 ?

Thanks

-- 
___________________________________
Franck Collineau
FTR&D DMI/TAM/TIL
4 rue du Clos Courtel
B.P. 59
35512 Cesson Sévigné Cedex
tél: 02 99 12 43 97
franck.collineau@francetelecom.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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2001-11-12 21:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-09 13:44 [Caml-list] Great Beginner Franck
2001-11-11 11:34 ` Remi VANICAT
2001-11-10 15:29   ` Diego Olivier Fernandez Pons
2001-11-12  6:39     ` Remi VANICAT
2001-11-12  8:13       ` Diego Olivier Fernandez Pons
  -- strict thread matches above, loose matches on Subject: below --
2001-11-09 14:10 Krishnaswami, Neel
2001-11-09 11:08 Franck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).