List Info

Thread: "ocaml_beginners"::[] The G.C.D. program of OCaml




"ocaml_beginners"::[] The G.C.D. program of OCaml
user name
2006-08-18 08:47:24
On Thu, 17 Aug 2006 15:32:02 +0200, LORENZO <arniwarpyahoo.com.tw> wrote:

>
>    Hi All,
>
>       I am new to functional programming
>       and try OCaml coding, but I don't get
>       understanding with my practice on G.C.D. as
below:
>
> (* Determine the greatest common divisor of
>    two positive numbers a and b. No need to assume
a>b  *)
>
> let rec gcd a b =
> (*  let r = a mod b in   *)
>     if (a mod b) = 0 then
>        Printf.printf "The G.C.D is %d\n" b
>     else
>        Printf.printf "processing r = %d\n"
(a mod b);
>        gcd b (a mod b);;
>
> let a = int_of_string Sys.argv.(1) in
> let b = int_of_string Sys.argv.(2) in
>   if !Sys.interactive then
>      ()
>   else
>      gcd a b;;
>
>      It always print "Fatal error: exception
Division_by_zero"
>      at the end of the program. Can anyone explain why
?

The Problem is that the recursive calll is not part of the
else statement,  
as you might thing based on your indentation. Correctly
indented your  
Program would be:


(* Determine the greatest common divisor of
    two positive numbers a and b. No need to assume a>b 
*)

let rec gcd a b =
(*  let r = a mod b in   *)
     if (a mod b) = 0 then
        Printf.printf "The G.C.D is %d\n" b
     else
        Printf.printf "processing r = %d\n" (a
mod b);
     gcd b (a mod b);;

let a = int_of_string Sys.argv.(1) in
let b = int_of_string Sys.argv.(2) in
   if !Sys.interactive then
      ()
   else
      gcd a b;;

As you can see now the recursive call is allways executed.
Instead you  
could write:

let rec gcd a b =
(*  let r = a mod b in   *)
     if (a mod b) = 0 then
        Printf.printf "The G.C.D is %d\n" b
     else
        begin
          Printf.printf "processing r = %d\n"
(a mod b);
          gcd b (a mod b)
       end
     ;;

let a = int_of_string Sys.argv.(1) in
let b = int_of_string Sys.argv.(2) in
   if !Sys.interactive then
      ()
   else
      gcd a b;;

now the call is actually part of the else clause.

HTH,
    Till
-- 
Precisely why all the above should be so is not clear, but
goes some way  
to explain why, on the disc, the Gods are not so much
worshipped as blamed.
   --Terry Pratchett (The color of magic)


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] The G.C.D. program of OCaml
user name
2006-08-19 14:49:33
    Hello,

        Thanks both replies from Sachin and Till,
        as mentioned I am new to functional coding,
        Current I am not begin to study pattern matching
        style yet, though it's really amazing me a lot!!

        The begin...end statement did make recursive
        call part of the else clause and figure out
        my silly question.  



--- In ocaml_beginners@yahoogroups.com, "Till
Crueger" <crueger...>
wrote:
>
> >
> >      I am new to functional programming
> >      It always print "Fatal error: exception
Division_by_zero"
> >      at the end of the program. Can anyone explain
why ?
> 
> The Problem is that the recursive calll is not part of
the else
statement,  
> as you might thing based on your indentation. 
> As you can see now the recursive call is allways
executed. Instead you  
> could write:
> 
> let rec gcd a b =
> (*  let r = a mod b in   *)
>      if (a mod b) = 0 then
>         Printf.printf "The G.C.D is %d\n"
b
>      else
>         begin
>           Printf.printf "processing r =
%d\n" (a mod b);
>           gcd b (a mod b)
>        end
>      ;;
> 
> let a = int_of_string Sys.argv.(1) in
> let b = int_of_string Sys.argv.(2) in
>    if !Sys.interactive then
>       ()
>    else
>       gcd a b;;
> 
> now the call is actually part of the else clause.
> 
> HTH,
>     Till
> -- 
> Precisely why all the above should be so is not clear,
but goes some
way  
> to explain why, on the disc, the Gods are not so much
worshipped as
blamed.
>    --Terry Pratchett (The color of magic)
>






Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )