Re: Advice on a program.

"'Mr. Herr' [email protected] [ocaml_beginners]" <[email protected]> Mon, 15 Feb 2016 10:14:53 +0100
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Hi,

see my answers below.

Max

On 15.02.2016 06:15, Douglas Lewit [email protected] [ocaml_beginners] wrote:
>  
> Thanks Gabriel.  It worked beautifully!!!
>
> Here's a question for you.
Remember, this is an almost completely public mailing list ;-)
> 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.
>
There are some issues with this code: convoluted, even ugly
---
let f x y = match y with
    y when not (y = 0.0) -> x /. y
  | _                    -> x
--- 
More idiomatic would be:

let f x = function
    y when not (y = 0.0) -> x /. y
  | _                               -> x


because this avoids binding y two times: as parameter, and in the match clause.
Be aware: match binds values or structures to names!
Then the use of the guard (when) does not make sense. Better:

let f x = function
    0.0 -> x
  | y   -> x /. y

Now this is concise!


> But what does _ mean in the context of:
>
> *let create_matrix i j initial =
> Array.init i (fun _ -> Array.make j initial);;*
>
The underscore binds a parameter you do not need, sort of thow away parameter.
> Thanks for sharing.  Much appreciated!
>
> On a totally different subject, what is the Ocaml compiler written in?  C?  Some
> flavor of Assembler?  Just curious.
>
See here the output of the quite famous cloc perl script, that counts lines of code.
Take the figures as an indication, not as THE truth.
strobel@s132-intel:/opt/opam/4.02.3/build/ocaml> cloc *
    2449 text files.
    2328 unique files.                                         
     708 files ignored.

http://cloc.sourceforge.net v 1.64  T=16.50 s (107.2 files/s, 21009.7 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
OCaml                          1253          25340          49003         190873
C                               271           5351           6021          35323
Bourne Shell                     22            417            773           5130
make                             92            918           1228           4411
Lisp                             11            630            660           4224
C/C++ Header                     77           1267           1749           4023
Assembly                         24            404           1248           3945
MUMPS                             7            208              1           2461
MATLAB                            5             44              0            647
awk                               1             16             13             88
Perl                              1              4             11             46
Bourne Again Shell                1              7             11             33
Fortran 77                        1              5              0             21
sed                               1              3             13             10
C#                                1              2              0              9
--------------------------------------------------------------------------------
SUM:                           1768          34616          60731         251244
--------------------------------------------------------------------------------


/Str.
> Thanks!
>
> Best,
>
> Douglas.
>
>
>
> On Sun, Feb 14, 2016 at 9:09 PM, Gabriel Scherer [email protected]
> <mailto:[email protected]> [ocaml_beginners]
> <[email protected] <mailto:[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]
>     <mailto:[email protected]>
>     [ocaml_beginners] <[email protected]
>     <mailto:[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 ( ) ;;
>     >
>     >
>     >
>     >
>     >
>     >
>
>
>