Re: what did I do wrong here ?
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAMNTtbom-7xPJ5F=0MHq+Pde43M3WS4nzGSPQRVzh=0Ck05bAw@mail.gmail.com> |
if a = 0 then a else a+ sum(a-1) 2014-10-14 19:09 GMT+02:00 Roelof Wobben [email protected] [ocaml_beginners] <[email protected]>: > > > it helps but still im a little bit confused. > > What I try to do it this : > > lets say I want to calculate this 1+2+3 > then first 2+3 must be calculated and the outcome will be 5 > then 5+1 must be calculated so the outcome will be 6. > > Now there are no arguments anymore so the outcome must be displayed. > > But if there are no arguments so sum then the answer must be 0 > > and I use this as a example and here a= is used : # let rec factorial a > = if a = 1 then 1 else a * factorial (a - 1);; > > Roelof > > > > Matthieu Dubuget [email protected] [ocaml_beginners] schreef op > 14-10-2014 18:49: > > > > Le 14/10/2014 18:33, Roelof Wobben [email protected] [ocaml_beginners] a > écrit : > > let rec sum a = if a = 0 then a = a + 0 else a = a + sum (a - 1) ;; > > > The two branches of your if structure are tests : > > - a = a + 0 which is true > - and a = a + sum (a - 1) > > So. sum should return a bool, right ? > > But in the same time, you are using sum as if it would return an int, > since you add sum(a - 1) toawith the+` function that takes to ints and > returns an int. > > Just get rid of the a =: the result of the function is either the true > branch of the if test, or the false branch. > > Hoping this will help. > > > -- > Matthieu Dubuget > Guide d’autodéfense numérique : http://guide.boum.org > > > > -- Alexandre