Re: Advice on a program.

"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Sun, 14 Feb 2016 23:15:43 -0600
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAM0XMJTASkkoVBYpm3259rL4m56=yPBwFE0ZytZftKhs_OEAHw@mail.gmail.com>
Thanks Gabriel.  It worked beautifully!!!

Here's a question for you.  Regarding _ I understand what _ means with
"match with" expressions, but what does it mean with functions.  So for
example:

let f x y = match y with
y when not (y = 0.0) -> x /. y
_ -> x ;;

If y is 0, just return x, otherwise return x divided by y.

But what does _ mean in the context of:


*let create_matrix i j initial =Array.init i (fun _ -> Array.make j
initial);;*

Thanks for sharing.  Much appreciated!

On a totally different subject, what is the Ocaml compiler written in?  C?
Some flavor of Assembler?  Just curious.

Thanks!

Best,

Douglas.



On Sun, Feb 14, 2016 at 9:09 PM, Gabriel Scherer [email protected]
[ocaml_beginners] <[email protected]> wrote:

>
>
> First:
>
> let createMatrix i j initial =
> let matrix = Array.make i ( Array.make j initial ) in
> for n = 0 to i - 1 do
> matrix.(n) <- Array.make j initial ;
> done ;
> matrix ;;
>
> could be written instead
>
> let create_matrix i j initial =
> Array.init i (fun _ -> Array.make j initial);;
>
> Second, I think you could solve your issue by explicitly flushing the
> output buffer. Instead of
>
> printf "%5i" arr.(i).(j) ;
>
> you could write
>
> printf "%5i" arr.(i).(j) ; flush stdout;
>
> or, equivalently
>
> printf "%5i%!" arr.(i).(j) ;
>
>
> On Sun, Feb 14, 2016 at 9:18 PM, Douglas Lewit [email protected]
> [ocaml_beginners] <[email protected]> wrote:
> >
> >
> > Hi everyone,
> >
> > I need advice on a program. I want the pause ( Thread.delay <float> )
> after
> > each number, but instead I'm getting the pause after each line.
> >
> > What am I doing wrong?
> >
> > Thanks!
> >
> > Best,
> >
> > Douglas.
> >
> > (* Ocaml program to generate a simple multiplication table or table of
> > products.
> >
> > * A great example of imperative programming in Ocaml. *)
> >
> >
> > open Printf ;;
> >
> >
> > print_string "Enter a positive integer: " ;;
> >
> >
> > let n = read_int ( ) ;;
> >
> >
> > let createMatrix i j initial = let matrix = Array.make i ( Array.make j
> > initial ) in
> >
> > for n = 0 to i - 1 do
> >
> > matrix.(n) <- Array.make j initial ;
> >
> > done ;
> >
> > matrix ;;
> >
> >
> > let arr = createMatrix n n 0 ;; (* Initialize the 2-dimensional array
> > needed to solve this problem. *)
> >
> >
> > for j = 0 to n - 1 do
> >
> > arr.(0).(j) <- j + 1;
> >
> > done ;;
> >
> >
> > for i = 0 to n - 1 do
> >
> > arr.(i).(0) <- i + 1;
> >
> > done ;;
> >
> >
> > for i = 1 to n - 1 do
> >
> > for j = 1 to n - 1 do
> >
> > arr.(i).(j) <- arr.(i).(0) * arr.(0).(j) ;
> >
> > done ;
> >
> > done ;;
> >
> >
> > print_newline ( ) ;;
> >
> >
> > for i = 0 to n - 1 do
> >
> > for j = 0 to n - 1 do
> >
> > printf "%5i" arr.(i).(j) ;
> >
> > Thread.delay 0.012 ;
> >
> > done ;
> >
> > print_newline ( ) ;
> >
> > done ;;
> >
> >
> > print_newline ( ) ;;
> >
> >
> >
> >
> >
> >
>
> 
>