From: "Issac Trotts" <ijtrotts@ucdavis.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] laconical input from a file for arrays and/or matrices
Date: Mon, 2 Feb 2004 07:54:06 -0800 [thread overview]
Message-ID: <20040202155406.GA5103@manzanita> (raw)
In-Reply-To: <D15343265276D31197BC00A024A6C110774275@EXS_BDC>
On Mon, Feb 02, 2004 at 11:42:18AM +0100, Khamenia, Valery wrote:
> Hi OCaMLers,
>
> here goes my usual way to input ASCII matrices from file in C++:
>
> // ------- start of fragment -----------
> #include <iostream>
> #include <vector>
> using namespace std;
>
> void input_matrix(vector<vector<double > >&vec) {
> int m, n;
> cin >> m;
> cin >> n;
> vec.resize (m);
> for( int i = 0; i<m; i++)
> vec[i].resize(n);
> for( int i = 0; i<m; i++)
> for( int j = 0; j<n; j++)
> cin >> vec[i][j];
> }
> // -------- end of fragment -------------
>
> then for matrix :
>
> 3
> 3
> 1 2 3
> 4 5 6
> 7 8 9
>
> stored in ASCII text file mymatrix.dat
>
> i just run my program:
>
> % myprogram < mymatrix.dat
>
> and it is done.
>
> Thus, the few lines of code and i can apply all math I need.
Here's a way to do it with camlp4. You get OCaml-style comments
for free from Genlex this way:
#load "camlp4o.cma";;
open Genlex;;
let input_matrix channel =
let read_number = parser
[< 'Int i >] -> float i
| [< 'Float f >] -> f in
let rec read_nums n =
if n = 0
then parser [< >] -> []
else parser [< head=read_number; tail=read_nums (n-1) >] -> head::tail in
let rec read_matrix_data m n =
if m = 0
then parser [< >] -> []
else parser
[< row = read_nums n; rest = read_matrix_data (m-1) n >] ->
(Array.of_list row) :: rest in
let read_matrix = parser
[< 'Int m; 'Int n; rest >] ->
let rows = read_matrix_data m n rest in
Array.of_list rows in
let charstream = Stream.of_channel channel in
let lexer = Genlex.make_lexer [] charstream in
read_matrix lexer;;
input_matrix stdin;;
--
Issac Trotts
http://redwood.ucdavis.edu/~issac
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
prev parent reply other threads:[~2004-02-02 15:54 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-02-02 10:42 Khamenia, Valery
2004-02-02 10:57 ` samsaga2
2004-02-02 14:20 ` Virgile Prevosto
2004-02-02 14:36 ` Richard Jones
2004-02-02 15:54 ` Issac Trotts [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040202155406.GA5103@manzanita \
--to=ijtrotts@ucdavis.edu \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox