Re: Reading in floats from strings???
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 27 Jan 2016 22:34:58 -0600
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJSmm6Bq4pxHZwWCkytpCTeJCARU_5zY0zwGkSudmSOEpQ@mail.gmail.com> |
Thanks guys. These were excellent suggestions, and none of them were that difficult to implement. Thanks again! Sorry about that other message. I didn't realize that the group doesn't allow messages with attachments. ( I guess that's for security reasons, but maybe I'm wrong. ) Just curious, but are there any colleges, universities, online schools, etc, that offer a program of study in Ocaml? Most of the CS programs in my city teach either C, C++ or Java. I don't know of any school around here that offers training in a functional programming language. On Wed, Jan 27, 2016 at 12:28 PM, Gabriel Scherer [email protected] [ocaml_beginners] <[email protected]> wrote: > > > My quick&dirty way to parse numeric values in string format is Scanf, > using formats of the kind " %d" (for integers) and " %f" (for floats), with > the space having the meaning of "ignore any space before". > > It is easier to do on the standard input directly: > > let read_next_float () = Scanf.scanf " %f" (fun x -> x) > > let () = > let x = read_next_float () in > let y = read_next_float () in > print_float (x +. y) > > if your input comes from a string, you have to convert it into a scanning > buffer first: > http://caml.inria.fr/pub/docs/manual-ocaml/libref/Scanf.html > http://caml.inria.fr/pub/docs/manual-ocaml/libref/Scanf.Scanning.html > > let read_next_float input = Scanf.bscanf input " %f" (fun x -> x) > > let sum_of_string str = > let input = Scanf.Scanning.from_string str in > let x = read_next_float input in > let y = read_next_float input in > (x +. y) > > Please be aware, however, that Scanf is best used for programs where you > control the input. It does not perform much input validation, and if you > want to provide a convenient user interface to your user and be robust to > formatting errors, you should use a more robust parsing strategy. I have > never personally been in the situation where regexp parsing felt like the > right compromise between expressivity and convenience, I tend to move to > full lexer+parser stacks directly (which are easy to use in OCaml, see > [sedlex] + [menhir]), but that's a question of habits I guess. > > sedlex: https://github.com/alainfrisch/sedlex > menhir: http://gallium.inria.fr/~fpottier/menhir/ > compiling a project using both: > http://stackoverflow.com/questions/30853159/using-menhir-with-sedlex > > On Wed, Jan 27, 2016 at 10:29 AM, Douglas Lewit [email protected] > [ocaml_beginners] <[email protected]> wrote: > >> >> >> Hi everyone, >> >> I am new to this list, so I thought I would introduce myself. Also, I >> have a question about how to read in data of the form "11.3 12.9". >> Obviously a string, but I need to extract the floats contained in the >> string. I know how to do that in Java and also in Python, but for me Ocaml >> is still pretty much a foreign language. I appreciate any guidance you can >> offer. >> >> Anyhow, I've got a master's in math from a small state university in >> Chicago, Illinois. I'm currently working on a second master's in CS at the >> same university. My work experience is mostly in teaching, although I've >> also done some retail management. I used to be a high school math teacher >> ( mostly Catholic high schools, but also have some experience in the public >> school system ) and currently I teach part-time at Oakton Community College >> ( near Chicago ). I'm interested in leaving education within the next >> couple of years to find a new career in the software industry. My age >> could be a factor that works to my disadvantage, since most of the people >> that I personally know in various startup companies are quite young. ( >> I've been to some Python and Ruby meetups in the Chicago area, and I get >> the distinct impression that the vast majority of attendees are under the >> age of 35. ) >> >> Ocaml is appealing to me for a couple of reasons. Well first off, how >> can I resist a book that has a picture of a lovable camel on its front >> cover!!! ;-) But on a more serious note, I like Ocaml because it really >> forces the programmer to get over his fear or hatred of recursion. I think >> recursion is sort of optional in many other languages, and in fact some >> programmers really hate it because it does require a different way of >> thinking about problems. But in Ocaml recursion is not optional. So for >> me it's a really good exercise in thinking recursively about various >> problems. Although sometimes I think Ocaml goes a little too far with its >> emphasis on recursion. I mean.... there are times when a nice little for >> loop or while loop would do the job with far greater simplicity! >> >> From the reading I have done on the web it appears that Ocaml is a big >> hit with Jane Street, a very large financial trading company with offices >> in NYC, London and Hong Kong. Outside of Jane Street, where is Ocaml used? >> >> Take care and thanks for reading my intro message. >> >> Best, >> >> Douglas Lewit >> >> P.S. Is there any connection between Ocaml and Wolfram Mathematica? >> Both begin and end comments with (* and *), respectively, and both >> emphasize the functional approach to programming. I'm a big fan of >> Mathematica, and often incorporate it into math classes that I teach. I'm >> just wondering if there's a connection between the two software projects. >> Thanks. >> >> >> >> > >